为多个域和SSL配置.htaccess

时间:2014-11-24 00:38:45

标签: .htaccess mod-rewrite ssl

我有一个主域“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]

1 个答案:

答案 0 :(得分:1)

您可以通过取消对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]

在您的问题中,您说您想将所有其他域重定向到此域的主页,但您的规则根本不会这样做。您的规则是将请求重定向到同一请求,但仅限于此域。如果您想保持这种逻辑,那么您需要在$1的末尾添加https://www.example.co.uk/