【问题标题】:Configuring .htaccess for multiple domains and SSL为多个域和 SSL 配置 .htaccess
【发布时间】:2015-01-21 15:48:08
【问题描述】:

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

【问题讨论】:

    标签: .htaccess mod-rewrite ssl


    【解决方案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]
    

    在您的问题中,您说您想将所有其他域重定向到该域的 主页,但您的规则根本没有这样做。您的规则将请求重定向到同一个请求,但只是这个域。如果要保留该逻辑,则需要在 https://www.example.co.uk/ 的末尾添加 $1

    【讨论】:

    • 感谢乔恩的回复。我对你答案的第二部分有疑问。我是否需要为每个其他域复制此声明,例如example2.co.uk 和 example3.co.uk 还是这个语句只是简单地接受所有请求并将它们重定向到 example.co.uk 的主页? # 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]
    • @user3220812 第二条规则说“任何不是“www.example.co.uk”的东西都会被重定向到“www.example.co.uk”,所以您应该申请所有其他 6 个不属于 www.example.co.uk 的域。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    相关资源
    最近更新 更多