如何修复由.htaccess引起的过多重定向?

时间:2013-01-26 03:44:21

标签: .htaccess redirect

我最近在客户的网站上添加了一个Wordpress博客。我创建了一个子目录并在那里安装了Wordpress。一切正常,直到我将.htaccess文件添加到根目录。

RewriteEngine on
rewritecond %{http_host} ^websitename.com [nc]
rewriterule ^(.*)$ http://www.websitename.com/$1 [r=301,nc] 

现在,当我点击博客链接时,我收到以下错误

The webpage at http://websitename.com/blog/ has resulted in too many redirects. 
Clearing    your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with 
your computer.

有人知道如何解决这个问题吗?我对.htaccess一点也不熟悉。

编辑:为了清楚起见,301重定向适用于所有页面,博客文件夹除外。如果您输入websitename.com,则会重定向到www.websitename.com。但是,如果您输入websitename.com/blog,您将遇到错误。此示例中的博客指向Wordpress文件夹。

6 个答案:

答案 0 :(得分:4)

我认为你试图强迫“www。”?

使用以下内容:

RewriteCond %{HTTP_HOST} ^suncoastlaw\.com
RewriteRule .* http://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]

要获得更多信息,请查看我在此处写的帖子:Forcing www

答案 1 :(得分:4)

你想要*.suncoastlaw.com/xx -> www.suncoastlaw.com/xx吗?

然后你的conf

^(.*)$ http://www.suncoastlaw.com/$1

让www.suncoastlaw.com/$1 301-> www.suncoastlaw.com/$1

因此会有太多的重定向

更改为

rewritecond %{http_host} !www.suncoastlaw.com

当没有www.suncoastlaw.com然后重定向

所有重写配置细节都可以在

找到

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

如何追踪

您的http://suncoastlaw.com/blog/重定向到http://www.suncoastlaw.com/blog/然后 http://www.suncoastlaw.com/blog/重定向到http://suncoastlaw.com/blog/ 这就是问题

$ curl -I http://suncoastlaw.com/blog/
HTTP/1.1 302 Found
Date: Sat, 26 Jan 2013 13:37:10 GMT
Server: Apache
Location: http://www.suncoastlaw.com/blog/
Content-Type: text/html; charset=iso-8859-1



$ curl -I http://www.suncoastlaw.com/blog/
HTTP/1.1 301 Moved Permanently
Date: Sat, 26 Jan 2013 13:37:21 GMT
Server: Apache
X-Pingback: http://suncoastlaw.com/blog/xmlrpc.php
Location: http://suncoastlaw.com/blog/
Content-Type: text/html; charset=UTF-8

答案 2 :(得分:1)

尝试将这些行添加到httpd.conf文件中,而不是使用.htaccess文件来强制WWW子域:

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^(127\.0\.0\.0|localhost) [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [L,R=301]

如果您只是通过IP访问您的网络服务器,您可能还想在第二行列表中添加您的IP。有关更多解释,请阅读this thread;)

中的答案

答案 3 :(得分:0)

问题突然发生。遗憾的是,我无法解决这个问题,但我找到了一个可以做到的人。显然,这个问题是由于我的数据库中的网站没有WWW。所以我的博客重定向到我原来网站的网址。通过添加WWW解决了这个问题。

答案 4 :(得分:0)

我遇到了同样的问题。问题不在于规则。规则是正确的但.htaccess中规则的顺序不是。这就是我之前所拥有的。注意我正在重写非www。

  #https
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  #remove home
  RewriteCond %{THE_REQUEST} ^.*/home
  RewriteRule ^(.*)home$ https://example.com/$1 [R=301,L] 


 # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
 #  slashes.
 # If your page resides at
 #  http://www.example.com/mypage/test1
 # then use
 # RewriteBase /mypage/test1/
 #RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php?/$1 [L]

 #301 redirect
 RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
 RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

我改为

 #301 redirect
 RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
 RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

 #https
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  #remove home
  RewriteCond %{THE_REQUEST} ^.*/home
  RewriteRule ^(.*)home$ https://example.com/$1 [R=301,L] 


 # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
 #  slashes.
 # If your page resides at
 #  http://www.example.com/mypage/test1
 # then use
 # RewriteBase /mypage/test1/
 #RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php?/$1 [L]

答案 5 :(得分:0)

RewriteEngine开启 rewritecond%{http_host} ^ websitename.com [nc] rewriterule ^(。*)$ http://www.websitename.com/ $ 1 [r = 301,nc]

替换

RewriteEngine开启 RewriteCond%{HTTPS} on RewriteRule(。*)https://% {HTTP_HOST}%{REQUEST_URI}