.htaccess ERR_TOO_MANY_REDIRECTS

时间:2016-03-01 17:40:37

标签: .htaccess url-rewriting rewrite

当我将此代码添加到ERR_TOO_MANY_REDIRECTS时,我在Chrome上遇到此错误.htaccess

# Redirect old Page URLs to new URLs
# Old URL format: http://example.com/?page=pagename
# New URL format: http://example.com/page/pagename
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^(.*)$ page/%1? [R=301,L]

# Redirect old Search URLs to new URLs
# Old URL format: http://example.com/search.php?search=keyword
# New URL format: http://example.com/search/keyword
RewriteCond %{REQUEST_URI} search.php
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ search/%1? [R=301,L]

# Redirect old Post URLs to new URLs
# Old URL format: http://example.com/post.php?id_post=1
# New URL format: http://example.com/post/1
RewriteCond %{REQUEST_URI} post.php
RewriteCond %{QUERY_STRING} ^id_post=([0-9]*)$
RewriteRule ^(.*)$ post/%1? [R=301,L]

# Support new SEO-friendly URLs
RewriteRule page/(.*) ?page=$1
RewriteRule search/(.*) search.php?search=$1
RewriteRule post/(.*) post.php?id_post=$1

你们可以告诉我这个错误吗,已经试图找出它但是找不到错误是什么。

1 个答案:

答案 0 :(得分:3)

您的规则在内部将目标网址重写为自身。您需要使用%{THE_REQUEST}来防止此行为,因为这些变量与内部重定向不匹配。

RewriteEngine on

#--Redirect from "/?page=foo" to "/page/foo"--#
RewriteCond %{THE_REQUEST} /\?page=([^\s]+) [NC]
RewriteRule ^ /page/%1? [NC,L,R]
#--Rewrite "/page/foo/" to "/?page=foo"--#
RewriteRule ^page/([^/]+)/?$ /?page=$1 [NC,L,QSA]

#--Redirect from "/search.php?search=foo" to "/search/foo"--#
RewriteCond %{THE_REQUEST} /search\.php\?search=([^\s]+) [NC]
RewriteRule ^ /search/%1? [NC,L,R]
#--Rewrite "/search/foo/" to "/search.php?search=foo"--#
RewriteRule ^search/([^/]+)/?$ /search.php?search=$1 [NC,L,QSA]