【发布时间】:2018-08-28 06:21:19
【问题描述】:
我有三个域:
example.comexample.netdomain2.example
我希望所有域(www/non-www/HTTPS/non-HTTPS 流量)重定向到 https://www.example.com。
我知道我需要先将example.net 和domain2.example(www/非www 和HTTPS)重定向到example.com,然后再重定向到https://www.example.com(SSL 证书仅适用于example.com 和www.example.com)。
.htaccess(整个文件):
RewriteEngine on
RewriteBase /
RewriteCond !{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]
RewriteRule ^/?$ "https\:\/\/www\.example\.com" [R=301,L]
RewriteCond !{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example.net [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.example [NC]
RewriteRule ^/?$ "http\:\/\/www\.domain1\.com" [R=301,L]
RewriteCond !{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]
RewriteRule ^/?$ "https\:\/\/www\.example\.com" [R=301,L]
RewriteCond !{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?example.net [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.example [NC]
RewriteRule ^/?$ "http\:\/\/www\.example\.com" [R=301,L]
# disable directory listing
Options -Indexes
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
只有example.com 重定向规则似乎工作正常,但example.net 和domain2.example 重定向不起作用。这样可以找工作吗?
【问题讨论】:
-
1) “先到 example.com 然后example.com”你不需要两个链式重定向,我什至建议再次和 2) 你不需要在重定向 URL 中转义字符,它不是一个正则表达式,所以
/和.就可以了。 3) 一般来说,对于重定向,我建议在 Apache 主配置中而不是在.htaccess中进行重定向,因为这将解决许多问题,您可以使用Redirect指令,这些指令足以满足更简单的重定向需求。