.htaccess将https:// www重定向到https://非www

时间:2018-01-24 11:20:04

标签: .htaccess redirect mod-rewrite

我有从所有域到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]

1 个答案:

答案 0 :(得分: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]

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