【问题标题】:.htaccess redirect https://www to https://non-www.htaccess 将 https://www 重定向到 https://non-www
【发布时间】:2018-07-03 10:12:53
【问题描述】:

我有一个从所有域到 https://example.de 的 301 重定向。 重定向适用于http://www.example,www.example 使用所有 tld (.com,.eu.net)。 但是对于https://www.example,它不会在没有 www 的情况下重定向到 https。 这是Mod重写:

RewriteEngine on

RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} ^www\.example\.de$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.de$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.at$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.at$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.eu$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.eu$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.org$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.org$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]

编辑: 到目前为止,以下工作

RewriteCond %{HTTPS} on [NC]
RewriteCond %{HTTP_HOST} !^example\.de$ [NC]
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteBase /
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]

【问题讨论】:

  • 投反对票,没有答案,我爱这个社区....

标签: .htaccess redirect mod-rewrite


【解决方案1】:

您的规则不会重定向 https 网址,因为您没有使用正确的重写条件。此外,您不需要编写多个条件来测试域名。而不是使用两条RewriteCond 行来测试www.example.comexample.com,您可以在类似的单一条件下进行测试。 RewriteCond %{HTTP_HOST} ^(www\.)?example.com$.

以下是您的规则的较短版本。

RewriteEngine on

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.at$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.org$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net$ [OR]
 # the main domain. redirect www.example.de to example.de
RewriteCond  %{HTTP_HOST} ^www\.example\.at$
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]

在测试此更改之前清除浏览器缓存。

【讨论】:

  • 谢谢你,我试过了,得到错误 500(脚本失败)
  • @Tobiass 有一个错字,我已经修正了。检查更新版本。
  • 谢谢我会在几分钟内测试一下,为什么上一个 RewriteCond 中域 example.at 而不是 example.de?​​span>
  • 完美,我认为它有效,我将在所有域中对其进行测试。你的代码更快吧?
猜你喜欢
  • 1970-01-01
  • 2012-06-09
  • 2019-10-26
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
  • 2016-10-14
  • 2019-12-04
相关资源
最近更新 更多