问题
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