【问题标题】:http to https redirecthttp 到 https 重定向
【发布时间】:2014-08-21 10:02:49
【问题描述】:

我尝试使用 .htaccess 实现以下行为:

重定向http://www.mydomain.tld -> https://mydomain.tld

还重定向我的旧链接,例如:

http://www.mydomain.tld/news/mynews 应该被重定向到https://mydomain.tld/news/mynews

该页面现在应该只能通过 https 访问而没有前导“www”。

我有这条规则可以从http://www.mydomain.tld 重定向到http://mydomain.tld

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>

强制 https

<IfModule mod_headers.c>
   Header set Strict-Transport-Security max-age=16070400;
</IfModule>

但是我很难弄清楚如何正确编写条件,这样我就不会失去建立链接的能力(重定向 301 很重要)。

希望任何人都可以帮助我! 谢谢!

【问题讨论】:

  • 您的[R=301,L] 将重定向定义为 301。您可以通过在浏览器中打开开发者工具的网络接口访问该站点来进行测试,其中会显示所有重定向。相关,但您应该在该规则中重定向到 https 而不是 http。另外作为提醒,因为人们错过了很多,您应该检查 any 子域,而不仅仅是 www,并重定向。这样,如果有人链接到 nonsense.yourdomain.com,它仍然会重定向,并且 Google 不会索引您的“新”子域。

标签: apache .htaccess mod-rewrite redirect


【解决方案1】:

我将所有内容都重定向到 https://example.com 而不使用 www。

这是我用的:

#Redirect www and http
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

从来没有遇到过这样的问题。

【讨论】:

    猜你喜欢
    • 2011-09-06
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2018-04-24
    • 2013-09-27
    相关资源
    最近更新 更多