Forum Moderators: phranque

Message Too Old, No Replies

Redirect to another webserver

         

burnside

4:34 am on Nov 10, 2010 (gmt 0)

10+ Year Member



Trying to find the correct rewrite rule to do the following:

If someone enters:

http://www.mysite.com/products/cup


it should go to:

http://www.store.com/cup


Thanks in advance!

g1smd

2:53 pm on Nov 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



.. ..

[edited by: g1smd at 2:55 pm (utc) on Nov 10, 2010]

g1smd

2:53 pm on Nov 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's more than ten thousand prior threads in this forum detailing things very similar to what you want to do.

What have you tried so far?

burnside

3:48 pm on Nov 10, 2010 (gmt 0)

10+ Year Member



Sorry man, I eventually got things working, but believe me, took over an hour of searching to find exactly what I needed and I still don't know if it is optimized for what I need. Here is what I finally used:

RewriteRule ^products/(.+)$ http://www.store.com/$1 [L]
RewriteRule ^account/admin/(.+)$ http://www.store.com/~admin$1 [L]


That second rule deals with allowing the admin account to be accessed with the store.com domain. Does all this look fine? It seems to be working but I don't know if I should have Rewrite Conditions before each one. Does it matter? I know that the [L] means Last Rule, but want to make sure that having no Rewrite Conditions is any cause for concern.

Thanks again.

g1smd

4:43 pm on Nov 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's a very credible first attempt, better than most!

As this is server configuration code it's really important that you understand how it works and what it does. Your hour of research was not at all wasted!

The rules each produce a 302 redirect. Add the [R=301,L] flag to fix this.

Add a new, third, rule for redirecting non-www to www. This domain canonicalisation code is important and widely posted here. This rule will be last in the list.

So, your new rules will be in a specific order. The first rules will process very specific requests, those with /products or /account/admin in. The last rule will be the general catch-all if no other previous rules matched.

Some requests will match none of the rules (such as www requests for the correct URL) and none of the rules will be activated.

Once you have the rules working, it is important that the pages of your site link to the "correct" URL for each page. It is not a good idea for a click on an internal navigation link to then result in a redirect within the site.

Another thing you will need to do is make sure you link to images, scripts and stylesheet files, using href="/path/file" format with a leading slash. It is the browser that evaluates the full URL for embedded objects. Using the href="../../path/file" linking format usually leads to failure.

[edited by: g1smd at 4:56 pm (utc) on Nov 10, 2010]

burnside

4:53 pm on Nov 10, 2010 (gmt 0)

10+ Year Member



Awesome. I get working on this now. Thanks for all the tips! So is it not imperative to have rewrite conditions?

g1smd

4:59 pm on Nov 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The major "condition" is the pattern found in the rule itself. If you only need to test the requested filepath part of the URL request then the pattern in the RewriteRule is all you need.

If you need to test the requested domain name, query string, server port, or anything else that is not purely the path part of the URL, only then do you need a RewriteCond too.

burnside

5:04 pm on Nov 10, 2010 (gmt 0)

10+ Year Member



Perfect. Makes sense.