【问题标题】:Redirect permanently all possible domains and subdomains to https without www (https://example.com)将所有可能的域和子域永久重定向到不带 www 的 https (https://example.com)
【发布时间】:2018-12-25 20:41:18
【问题描述】:

我的网站流量很大,因此性能至关重要。我正在使用 apache 和 ubuntu。

我正在尝试将所有可能的域可能性重定向到 https://example.com,包括所有子域 (https://test.example.com)。

这是我目前拥有的:

RewriteEngine On

# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]

# match urls that are non https (without the www)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我觉得上面的代码正在做额外的重新路由/工作。在虚拟主机文件或 .htaccess 中是否有更直接的解决方案(以获得更快的性能)?

谢谢!

【问题讨论】:

    标签: apache performance .htaccess mod-rewrite https


    【解决方案1】:

    您可以结合所有三个条件

    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
    RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
    

    解释:如果not https OR not example.com 然后重定向到https://example.com/xxx

    回答您关于性能的问题:在.htaccess 中重写可能比在 Apache 配置文件中重写慢。

    【讨论】:

    • 谢谢你,但是如果我想把它放在我的 apache 配置文件中应该写什么呢?
    • 另外,subdomain.example.com 重定向到 https://example.com 但它应该重定向到 https://subdomain.example.com
    • 这里是一个例子:tltech.com/info/rewriterule-in-htaccess-vs-httpd-conf。对于subdomain,我有点惊讶,因为您在问题中提到了everything。提醒我你的规则方案,只是为了确保我们在同一个波长上
    猜你喜欢
    • 2018-06-02
    • 2018-06-01
    • 2019-02-21
    • 2017-04-22
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多