Forum Moderators: phranque

Message Too Old, No Replies

Rewrite a URL twice

         

burcot

1:06 pm on Sep 29, 2006 (gmt 0)

10+ Year Member



Hi

I recently managed to rewrite my sites' dynamic urls to statics using JD Morgan's guide.

This works using...

rewriteRule ^c-([^/]+)/?.htm$ /categories.php?cat=$1 [L]

rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /categories\.php\?cat=([^\ ]+)\ HTTP/
rewriteRule ^categories\.php$ [mysite.co.uk...] [R=301,L]

As a rule, this is absolutely fine. However in some instances, i would like to rewrite individual url rewrites again (rewrite a url twice).

In doing so, I would be rewriting the newly generated url:
[mysite.co.uk...]

to

[mysite.co.uk...]

I have attempted various things but to no avail.

Has anybody got any suggestions? All ideas would be greatly appreciated.

Cheers

jdMorgan

7:16 pm on Sep 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't confuse rewrites and redirects. When your current rule matches, it generates a 301-Moved Permanently redirect due to the [R=301] flag, and then the [L] flag terminates the mod_rewrite processing immediately. Therefore, a 301 response is sent to the client (e.g browser), telling it to re-request the resource from the new URL. This terminates the current HTTP transaction, and it is up to the client to ask for the desired content again, using the URL provided in the 301 response.

Therefore, you can't "rewrite" the URL again, because the current HTTP transaction has ended. Nothing will happen until the client sends a new request (probably using the new URL you sent to it). When the client re-issues the request, you will see the address bar change to this new URL.

I also don't know why you want to do a redirect there, as it doubles the load on your server, doubles the visitors' page load times, and 'exposes' your SE-unfriendly script URLs.

However, regardless of that, don't rewrite twice. Instead, put the more-specific rewriterules first, so that your 'special' URLs are rewritten to the correct URL all-at-once in a single step. Then let your current rule pick up and rewrite/redirect the remaining URLs, as desired.

For the sake of completeness, an internal rewrite changes only the server filepath associated with a requested URL, and occurs completely within the server and within the context of the current HTTP transaction; The client will be unaware that any change has taken place.

Jim

g1smd

9:40 pm on Oct 4, 2006 (gmt 0)

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



A "redirection chain" from A to B to C could easily confude a search engine bot.

Try to avoid doing that at all costs.