Forum Moderators: phranque

Message Too Old, No Replies

Redirect to same subdomain

         

joaomorais12

1:06 pm on Feb 14, 2007 (gmt 0)

10+ Year Member



Greetings,

After searching for awhile and not finding a solution, I decided to post.

Maybe I'll just start with an example:

I'm trying to do something like [subdomain.example.com...]

to redirect to [subdomain.example.com...]

(Notice that it's the same subdomain)

So far, I've just managed do this if I redirect to another subdomain (or another domain)

RewriteCond %{HTTP_HOST} ^subdomain.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/index.php?some_action=$1 [R=301,L]

If I use the same subdomain, it will give me an infinite loop.

Anyone had a similar problem?

Thx in advance.

jdMorgan

2:07 pm on Feb 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's quite possible there are two errors here, at different levels.

First, there is nothing in your code to prevent an infinite loop: Requests for "/index.php?some_action=n" will be redirected repeatedly.

Second, it's unlikely you need or want an external redirect, since this will result in the /index.php?some_action URL being listed by search engines, and replacing your static URLs. This is almost never what is desired. It is far more common to use an internal rewrite to pass static URL requests to scripts.

To fix both problems, I'd suggest:


RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule (.*) /index.php?some_action=$1 [L]

Instead of redirecting, this code simply takes any request for any resource (except for /index.php) and rewrites it to /index.php with the URL-path passed as the "some_action" parameter. This is done within the context of the current HTTP request, and does not force the client (browser, SE robot) to re-request the resource from the dynamic URL using a second HTTP request. Because of this, it does not 'expose' your dynamic URLs to search engines.

You may also wish to exclude requests for /robots.txt, /w3c/p3p.xml, images, external JavaScript and CSS files, and other "non-scripted" resources.

Jim

joaomorais12

10:45 am on Feb 16, 2007 (gmt 0)

10+ Year Member



Thx for the quick reply and I'm sorry for the late response.. these have been some busy, busy days..

I've tried the code you supplied as it makes sense for the application I'm building, but it results in a strange behavior.. As I try to use:

[subdomain.example.com...]

the webpage really slows down (as if it was on a infinite loop) but Apache logs don't show any kind of errors. The .htaccess has only the code you supplied.

Any ideas why the webpage is behaving like this with this code?

Thank you again

jdMorgan

3:50 pm on Feb 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Any ideas why the webpage is behaving like this with this code?

Sorry, no. Check the server error log, and try testing with a very simple php script to see if the problem is with the rewrite or with the script.

Jim

joaomorais12

7:27 pm on Feb 19, 2007 (gmt 0)

10+ Year Member



Ok. I'll try to test it just like you said, or maybe change the way I'm implementing it. :)

If I come up with a solution, I will post it here.

Thank you very much for your help. I really appreciated it.