.htaccess ERR_TOO_MANY_REDIRECTS

.htaccess ERR_TOO_MANY_REDIRECTS

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

# 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=
RewriteRule search/(.*) search.php?search=
RewriteRule post/(.*) post.php?id_post=

请问各位大神,请问是哪里出错了,已经试过了,但是没找到错误是什么。

您的规则正在内部将目标 url 重写为自身。您需要使用 %{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= [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= [NC,L,QSA]