【问题标题】:Most efficient htaccess url redirects?最有效的 htaccess url 重定向?
【发布时间】:2018-04-02 19:32:01
【问题描述】:

我正在寻找将传入网址重定向到我们网站的 https: 版本的最有效方法。我们现有的工作(见下文),我只是想知道是否有更好的方法来做到这一点,并尽量减少重定向的数量。

重定向应涵盖以下情况:

(Domain level)
http://www.example.com - > https://www.example.com
http://www.example.com/dir -> https://www.example.com/dir
http://example.com -> https://www.example.com
http://example.com/dir -> https://www.example.com/dir
www.example.com -> https://www.example.com
www.example.com/dir -> https://www.example.com/dir
example.com -> https://www.example.com
example.com/dir -> https://www.example.com/dir

(Subdomains)
http://subdomain.example.com -> https://www.example.com/subdomain
subdomain.example.com -> https://www.example.com/subdomain

这是我们在 htaccess 文件中的内容:

# Rewrite subdomains to folders, add www if not present
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

# Rewrite anything not previously caught
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

提前致谢!

【问题讨论】:

    标签: .htaccess url redirect


    【解决方案1】:

    根据您的规则,您将 main-domainsub-domain 强制转换为 https,但仅将 main-domainnon-www 转换为 wwww 所以,要让它对两者都有效:

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

    注意:清除浏览器缓存测试

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多