2

我目前对我的 htaccess 文件使用以下代码。它重定向到https://www.example.com

#if not forum.example.com and not ssl then redirect
RewriteCond %{HTTP_HOST} !^forum\. 
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R,L]

#redirect if https://forum.example.com
RewriteCond %{HTTP_HOST} ^forum\.
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://forum.%{HTTP_HOST}/$1 [R,L]

我遇到的问题是当我去

http://www.example.com

,此代码使其重定向到

https://www.www.example.com 

代替

https://www.example.com.

有谁知道我该如何解决这个问题?

如果您需要其他信息来提供帮助,请告诉我。

4

1 回答 1

1

如果满足 2 个否定条件,则执行第一个重写规则: Notforum和 not SSL,但重写总是插入www,即使它已经在 URL 中。之前没有检查过。

我认为在不修改实际规则的情况下纠正问题的最佳方法,除了从第一个替换中删除www之外,是在开头添加另一个规则,如下所示:

#non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com/?$ 
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

#if not forum.example.com and not ssl then redirect
RewriteCond %{HTTP_HOST} !^forum\. 
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L] 

#redirect if https://forum.example.com
RewriteCond %{HTTP_HOST} ^forum\.
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://forum.%{HTTP_HOST}/$1 [R,L]

What the added rule does, is to rewrite any URL without www to one with www. The resulting URL is then presented to the other rules.

I don't know if these 2 last rules work as intended, I guess they do. I am just trying to solve the www duplication issue in the question.

Hope this helps.

于 2012-12-04T05:31:42.157 回答