Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite - Query regarding 'internal' redirects

Mod_Rewrite only working when forcing external redirect [R]

         

Andy_I

7:21 pm on Oct 12, 2009 (gmt 0)

10+ Year Member



Hi Jim,

You put in an awful lot of hours helping people out with their mod_rewrite queries! Hopefully my query should be quite straightforward for you or other experts out there.

Without going into all the background, I have the following line in my .htaccess file:

RewriteRule ^manuf/alfa/diesel-tuning\.htm$ /channel/diesel-tuning2.php?id=1 [R]

This successfully re-directs from this URL:

http://www.example.com/manuf/alfa/diesel-tuning.htm

to:

http://www.example.com/channel/diesel-tuning2.php?id=1

The problem comes when I get rid of the [R] (as I want this re-direct to be transparent). It does successfully load in the 'diesel-tuning2.php' page, but images are missing, CSS hasn't been applied, etc. Further investigation shows that even though I've re-directed away from the (non-existent) 'manuf/alfa/' folder, the PHP page still thinks that I'm 'in' that folder, so non of the document relative links work.

I've trawled through tons of documentation on mod_rewrite, but I can't find anything that explains this behaviour.

Any ideas anyone?

Andy

jdMorgan

9:56 pm on Oct 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The most common reason for this is that the links to images, CSS, and external JS scripts on your pages are in 'page-relative' form.

It is the browser which resolves relative links to the canonical URLs that it must request in order to fetch these included objects. It does this resolution based on the 'current directory' showing in its address bar.

So, on your page with URL "example.com/manuf/alfa/diesel-tuning.html", a tag of <img src="timing-light.jpg"> will be resolved to a canonical URL of "http://example.com/manuf/alfa/timing-light.jpg". And since your rule only internally-rewrites the .html page request and not the requests for the objects included on that page, the requested image must reside in a physical filepath corresponding to /manuf/alfa/timing-light.jpg on your server, else the browser will see a 404-Not Found error and fail to include or render the object.

The simplest fix is to change all object includes on your pages to specify a server-relative URL-path rather than a page-relative URL-path. So the example link to the image above must change from <img src="timing-light.jpg"> to <img src="/path-to-image-directory/timing-light.jpg"> or to <img src="http://example.com/path-to-image-directory/timing-light.jpg">

A similar change will be needed for the CSS and external JS file includes.

Jim

Andy_I

10:16 pm on Oct 12, 2009 (gmt 0)

10+ Year Member



Jim,

I wish I'd posted my query up there 14 hours earlier, which is the time I've been struggling with this!

Your response sounds to make perfect sense. I suppose I'm surprised that I haven't seen anyone else struggling with this.....but perhaps everyone else understands how these things work!

Thanks so much for helping me out with this, particularly for showing the examples, which I really needed for this to sink in.

Andy

jdMorgan

10:44 pm on Oct 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, if you'd searched WebmasterWorld for "rewriterule breaks images OR css", you would have likely found at least some of the scores of other previous threads on this subject here... :)

It's a common problem, and not at all obvious until the point about browser URL resolution is understood.

Jim

g1smd

11:27 pm on Oct 12, 2009 (gmt 0)

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



Other points to note:

R gives a 302 redirect. Use R=301 whenever you want a 301 redirect.

Add the [L] flag to the end of the rule, unless you know the exact reason why it should be omitted.

For a redirect always add the domain name to the rule, to prevent issues with redirecting to the wrong domain name when CanonicalName is ON. Omit t he domain name when you need a rewrite, because adding it makes a redirect even if you do not specify the [R] flag.

Andy_I

9:09 am on Oct 13, 2009 (gmt 0)

10+ Year Member



g1smd,

Many thanks for the additional info.

Also worth noting that to prove my understanding of Jim's reply, I put in additional rules to handle the re-direction of the subsequent requests for img files, etc., and this worked successfully.

Andy