【问题标题】:Redirect all non-www to www for both http and https with .htaccess使用 .htaccess 将所有非 www 重定向到 http 和 https 的 www
【发布时间】:2014-03-02 12:20:30
【问题描述】:

我想将所有非 www 重定向到 www - 如果请求是通过 http 它应该重定向到 http://www.domain.com,如果请求是通过 https 然后直接到 https://www.domain.com

我在 .htaccess 中尝试了以下操作,但它会将所有内容重定向到 https

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301

我已经使用了这段代码,并且可以完美解决。请检查是否正确。

RewriteEngine On
RewriteBase /
#Redirect non-www to www

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.pnrstatusbuzz.in/%{REQUEST_URI}

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite redirect


    【解决方案1】:

    您可以将此规则用作您的第一条规则

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{HTTPS}s on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    【讨论】:

    • 经过大量试验和错误后,这对我也有用。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 2015-02-16
    相关资源
    最近更新 更多