为多个域和SSL配置.htaccess
我有一个主域“example.co.uk”和6个其他域+1个IP地址指向同一个文件夹根 我想对主域使用HTTPS,即将所有其他域重定向到此域的主页(避免SEO问题和example.com域和IP地址的内容重复) 如何使用当前的.htaccess文件实现这一点为多个域和SSL配置.htaccess,.htaccess,mod-rewrite,ssl,.htaccess,Mod Rewrite,Ssl,我有一个主域“example.co.uk”和6个其他域+1个IP地址指向同一个文件夹根 我想对主域使用HTTPS,即将所有其他域重定向到此域的主页(避免SEO问题和example.com域和IP地址的内容重复) 如何使用当前的.htaccess文件实现这一点 RewriteEngine on #Redirect IP address to example.co.uk. RewriteCond %{HTTP_HOST} ^12\.34\.567\.890 RewriteRule (.*) htt
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主机的条件检查进行否定,可以减少规则的数量。然后,您只需要使用规则来检查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/
谢谢你的回复,乔恩。关于你回答的第二部分,我有个问题。我是否需要为其他每个域(例如example2.co.uk和example3.co.uk)复制此声明,或者此声明只是将所有请求重定向到example.co.uk的主页<代码>#将所有其他域重定向到主页RewriteCond%{HTTP#u HOST}^www\.example.co.uk$[NC]重写规则^(.*)$https://www.example.co.uk/ [L,R=301]@user3220812第二条规则是“任何不是“www.example.co.uk”的内容都会被重定向到“www.example.co.uk”,因此所有其他6个不是www.example.co.uk的域都应该被应用。