luckr
luckr

Reputation: 73

.htaccess ERR_TOO_MANY_REDIRECTS

I got this error ERR_TOO_MANY_REDIRECTS on chrome when I add this code to my .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

Could you guys tell me wheres the error please, already tried to figure it out but couldn't find what the error is.

Upvotes: 1

Views: 330

Answers (1)

Amit Verma
Amit Verma

Reputation: 41249

Your rules are internally rewriting the target url to itself. You need to use %{THE_REQUEST} to prevent this behavior as these variables do not match internal redirects.

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]

Upvotes: 2

Related Questions