【问题标题】:http to https redirect with subdomains and www conversion via htaccesshttp 到 https 重定向与子域和 www 转换通过 htaccess
【发布时间】:2018-05-15 23:12:13
【问题描述】:

我需要对特定子域的每次调用。example.com.xx 在开头添加 www,然后将其重定向到 https。

htaccess redirect to https://www 有一个解决方案,但不适用于 sub.example.com.xx 重定向到 https://www.sub.example.com.xx

RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

如果我输入http://sub.example.com..,我会得到https://sub.example.com.xx 而不是https://www.sub.domain.com.xx

即使有http://yyy.sub.example.com.xx也必须给我https://www.yyy.sub.example.com.xx

例子:

example.com.xx ---> https://www.example.com.xx
http://example.com.xx ---> https://www.example.com.xx
https://example.com.xx ---> https://www.example.com.xx

sub.example.com.xx ---> https://www.sub.example.com.xx
http://sub.example.com.xx ---> https://www.sub.example.com.xx
https://sub.example.com.xx ---> https://www.sub.example.com.xx

other.sub.example.com.xx ---> https://www.other.sub.example.com.xx
http://other.sub.example.com.xx ---> https://www.other.sub.example.com.xx
https://other.sub.example.com.xx ---> https://www.other.sub.example.com.xx

【问题讨论】:

    标签: .htaccess redirect https subdomain


    【解决方案1】:

    你可以使用:

    # redirect to www
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
    
    # Redirect http to https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
    

    首先访问www,以避免双重重定向。

    【讨论】:

    • 我已经编辑了我的问题。您的最后一个示例至少适用于 sub.example.com.xx 。谢谢 。我必须测试其他情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 2016-01-20
    • 2018-08-10
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    相关资源
    最近更新 更多