【问题标题】:.htaccess rewrite condition to remove 'www' from 'https://www'.htaccess 重写条件以从“https://www”中删除“www”
【发布时间】:2015-06-09 03:10:06
【问题描述】:

我只需要在每种情况下添加“https”删除“www”:

我正在使用以下代码:

    RewriteEngine on
    RewriteCond %{http_host} ^www\.example\.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,NC]

    RewriteCond %{HTTPS} !on
    RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [R,L]

无论出于何种原因,它适用于前两种情况,但我无法让它适用于最后一种情况。

我什至无法解决最后一个孤立的案例,例如喜欢:

RewriteEngine on
RewriteCond %{https_host} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,NC]

非常感谢任何帮助!

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    我确定这是重复的,但这里是我使用的代码:

    RewriteEngine on
    
    #This removes the www.
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [L,R=301,NC,QSA]
    
    #This makes redirect to https
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    【讨论】:

    • 对我有用,希望对我有帮助。
    • 太棒了! - 这对我也有效。我尝试了很多在 stackoverflow 上发布的解决方案,但没有一个对我有用…… .htaccess 规则是否以某种方式缓存?您的解决方案没有立即奏效……它花了大约 2 分钟才顺利工作。
    • 这仅适用于 google chrome,不适用于 edge 或 firefox。我的意思是第四个“example.com”->“example.com”//不工作
    • 是的缓存会让你觉得这不起作用。清除缓存或使用 F12 -> 网络 -> 禁用缓存。
    猜你喜欢
    • 1970-01-01
    • 2014-12-16
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2011-09-17
    • 2018-07-03
    • 1970-01-01
    相关资源
    最近更新 更多