Forum Moderators: phranque

Message Too Old, No Replies

Strange rewrite behavior

         

Joe Belmaati

9:53 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



I am trying to create an internal rewrite to send requests for mydomain.com/index.php to mydomain.com/dir/index.php

# Redirect to dir
RewriteCond %{REQUEST_URI} index.php
RewriteRule .* /dir/index.php [R]

But this freezes the browser and I get an error that the number of redirects have been exceeded. Interestingly, if I do this:

# Redirect to dir
RewriteCond %{REQUEST_URI} index.php
RewriteRule .* /dir/test.php [R]

then all works to perfection. Is there a way that I would be able to accomplish what I am trying to do...?

Sincere thanks,
Joe Belmaati
Copenhagen Denmark

PS. Did a board search but I could not find the information.

jdMorgan

10:34 pm on Dec 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is not strange at all; You've created an infinite loop, since your rewritten URL will match the requirements to rewrite again, once the browser requests the new URL after the redirect.

Do you really want a redirect and not an internal rewrite? That is, do you want the new URL to show up in search engine results and the visitor's browser address bar?

The code problem is easy to fix, but you need to decide about the redirect versus rewrite, and what you're really trying to accomplish.


RewriteCond $1!dir/index\.php$
RewriteRule (.*) /dir/index.php [R,L]

This fixes your looping problem, but it will also make all files on your server inaccessible, except for /dir/index.php. This includes all images, scripts, external CCS & JS, robots.txt file, etc. I doubt that's what you really want. So... in order to redirect only requests for yourdomain.com/index.php, try:

RewriteRule ^index\.php$ /dir/index.php [R,L]

Jim

Joe Belmaati

10:48 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Thanks a ton, Jim! I suppose the perfect thing would be an invisible redirect - I do run into problems with missing stylesheets, wrong log in paths and things of that nature though. Thank you very much one again :D

Joe Belmaati

10:50 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



RewriteRule ^index\.php$ /dir/index.php [R,L]

...still seems to be looping..

jdMorgan

11:46 pm on Dec 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the code in your root directory, or in /dir?

It's intended to work in the root (home page) directory.

Jim

Joe Belmaati

6:41 am on Dec 7, 2004 (gmt 0)

10+ Year Member



Yes, it is in my root directory.

jdMorgan

1:42 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, you've got me stumped. You may have code or settings in other .htaccess files or in httpd.conf that is causing this simple rewrite to fail.

Look for any other redirects that may conflict with this one, or settings in your /dir subdirectory such as RewriteOptions inherit that may cause a loop.

Jim