我有一个主域“example.co.uk”和 6 个其他域 + 1 个指向同一文件夹根目录的 IP 地址。
我想对主域使用 HTTPS,即 https://www.example.co.uk并将所有其他域重定向到该域的主页(避免 SEO 问题和与 example.com 域和 IP 地址的内容重复)
如何使用我当前的 .htaccess 文件实现此目的?
RewriteEngine on
#Redirect IP address to example.co.uk.
RewriteCond %{HTTP_HOST} ^12\.34\.567\.890
RewriteRule (.*) https://www.example.co.uk/$1 [R=301,L]
# Redirect example.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example2.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example3.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example3\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example4.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example4\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example5.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example5\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example6.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example6\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
最佳答案
您可以通过取消对 HTTP HOST 的条件检查来减少规则的数量。然后,您只需要检查 HTTPS 的规则:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [L,R=301]
# redirect all the other domains to the homepage
RewriteCond %{HTTP_HOST} !^www\.example.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/ [L,R=301]
在您的问题中,您说您想将所有其他域重定向到该域的主页,但您的规则根本没有这样做。您的规则将请求重定向到同一个请求,但只是这个域。如果您想保留该逻辑,则需要在 https://www.example.co.uk/
的末尾添加一个 $1
。
关于.htaccess - 为多个域和 SSL 配置 .htaccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27096251/