homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

htaccess prob
redirect loop

 5:35 pm on Feb 19, 2009 (gmt 0)

Hi guys,
I have a problem with a htaccess file.

On my site i rewrite urls like so

RewriteRule ^([A-Za-z0-9_-]+)\.html$ /index.php?section=$1&%{QUERY_STRING} [L]

this works fine, the problem is that i want to redirect pages from my old site when it goes live to the index page. these pages on the old site look like


so i want to say

RewriteRule ^index.php /index.html [R=301,NC,L]

problem is that when the .html rewrite happens it redirects to index.php which is then redirected back to index.html the back to php.

Is there any way i can get round this? the only way i can think of at the moment is to rename my index.php page to site.php but this seems a bit drastic.

Any help massively appreciated.


[edited by: Scally_Ally at 5:35 pm (utc) on Feb. 19, 2009]



 10:44 pm on Feb 19, 2009 (gmt 0)

Check the initial client request to stop the loop:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php[^\ ]*\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/index.ht[b]ml?[/b] [R=301,L]

This prevents redirecting index.php to index.html if the request for index.php is the result of the action of your internal rewrite rule; Only direct client requests for index.php will get redirected to index.html.

Note that I recommend dumping the "index.html" if possible, and simply redirecting to "www.example.com/". Try this after testing the rule as written above.

This redirect rule should go before your general domain canonicalization redirect (if you have one), and all such external redirect rules should be placed before all of your internal rewrite rules.


[edit] Corrected code to fix problem reported below. [/edit]

[edited by: jdMorgan at 4:19 pm (utc) on Feb. 24, 2009]


 12:57 pm on Feb 24, 2009 (gmt 0)

Thanks alot for you help bill - I still have so much stuff to learn with htaccess..

this worked well in stopping the infinite loop, the only problem that i have now is that the querystring then gets attached to the index.html page. I have done alot of research but cannot find how to stop the querystring getting passed across.

The redirect at the moment goes
goes to

I just want it to go to
without the query string.

Many thanks again.


 4:21 pm on Feb 24, 2009 (gmt 0)

Yeah, I always forget to tell the rule to remove the query. Hopefully no-one else has copied the original code. :(

See the corrections in my post above.



 8:33 pm on Feb 24, 2009 (gmt 0)

I would usually redirect to a URL that does not include the index file filename in the target, the target URL then simply ending with a trailing slash.


 9:56 am on Feb 26, 2009 (gmt 0)

Thanks a lot for you help Jim, works perfectly now. (who would have thought a question mark could do so much..)

I have taken your advice g1smd - my rewrite now looks like

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php[^\ ]*\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/? [R=301,NC,L]

So it doesnt incorporate the index.html

Thanks again


 11:11 pm on Feb 26, 2009 (gmt 0)

That will work fine as long as the right index file filename is mentioned in the
DirectoryIndex directive.

By the way, that code directly above is for a redirect, not a rewrite.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved