一个域到一个域的多重变化

时间:2018-06-27 07:44:03

标签: apache mod-rewrite url-rewriting

我试图编写代码来永久重定向多个变体 例如。

http://www.example.com
http://example.com

http://www.example.com/index.php

http://example.com/index.php

https://example.com

我只有几行代码

 RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com/index.php$
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

对我来说很好用,但是所有这些代码行都可以用单行还是两行编写?

1 个答案:

答案 0 :(得分:1)

我认为您应该只拥有RewriteCond %{HTTP_HOST} .*example.com$,而没有任何^

这应该涵盖您的第一行和第二行HTTP_HOST。

其中包含/index.php的HTTP_HOST第三行很糟糕,因为/index.php绝不是HTTP_HOST的一部分。

也不要一直重复RewriteEngine On,也不需要在端口80上设置条件。(如果端口443上有HTTPS,您仍然希望此重定向,对吗?)

所以最后:

RewriteEngine On 
RewriteCond %{HTTP_HOST} .*example\.com$
RewriteRule ^(.*)$ https://example.com/$1 [R,L]