将HTTPS域重写为另一个HTTPS域 - htaccess

时间:2016-08-26 17:47:55

标签: apache .htaccess redirect mod-rewrite

目前我有

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^domain2\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^www\.domain2\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]

将www和非www domain.com重定向到https://domain.com 并将www和非www domain2.com重定向到https://domain.com domain2是一个停放的域名。

但您仍然可以访问https://domain2.comhttps://www.domain2.com 我想将这些重定向到https://domain.com 请指教,谢谢

2 个答案:

答案 0 :(得分:1)

您可以合并其中一些规则并修改domain2 -> domain的重定向规则,以便始终重定向到https://domain.com

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]

RewriteCond %{HTTP_HOST} ^(?:www\.)?domain2\.com$ [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,NE,L]

答案 1 :(得分:0)

应该这么简单:

RewriteCond %{REQUEST_SCHEME} !https [NC,OR]
RewriteCond %{HTTP_HOST} !^domain.com$
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,NE,L]

不过,请记住@anubhava关于为domain2.com配置VirtualHost和SSL的最后评论