Welcome to WebmasterWorld Guest from 54.221.28.179

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess prob

redirect loop

     

Scally_Ally

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

10+ Year Member



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

index.php?var=myvar

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.

Ally

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

jdMorgan

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

Jim

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

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

Scally_Ally

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

10+ Year Member



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
http://www.example.com/index.php?cat=sdasasas
goes to
http://www.example.com/index.html?cat=sdasasas

I just want it to go to
http://www.example.com/index.html
without the query string.

Many thanks again.
Ally

jdMorgan

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

Jim

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

Scally_Ally

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

10+ Year Member



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
Ally

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month