Redirect (301) from domain.com to www.domain.com, while also forcing HTTPS in HTACCESS

ε祈祈猫儿з 提交于 2019-12-08 05:27:28

问题


Ok, so this is what I have originally , which redirects any domain.net or www.domain.net to www.domain.com (with SSL).

RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^domain\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.net$
RewriteRule ^/?$ "http\:\/\/domain\.com" [R=301,L]

Now, I want to force all of these conditions to report 301 to (for search engine purposes): domain.net www.domain.net domain.com

and force all conditions to: www.domain.com WITH SSL (even somebody typing http://www.domain.com should reach https://www.domain.com).

Does that make sense? Here's what I've got so far, which I think will work, but I don't know how to add in the 301 part...

RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^domain\.net$ [OR]
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.net$
RewriteRule ^/?$ "http\:\/\/domain\.com" [R=301,L]

回答1:


You say you want to use only www.domain.com, but you code says you only want to use domain.com. Anyway, this should do the trick:

RewriteEngine On
RewriteBase /

# redirect any domain other than www.domain.com to www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

# force https on www.domain.com
RewriteCond %{HTTPS} ^off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]


来源:https://stackoverflow.com/questions/8770545/redirect-301-from-domain-com-to-www-domain-com-while-also-forcing-https-in-ht

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!