Using rewrite rules without breaking page relative links

Topics: User Forum
Feb 20, 2007 at 11:01 PM
Is there a way to use or write your rewrite rules so that they don't break page relative links?

I want to be able to rewrite domain.com/care to domain.com/custom/index.asp, but I want the images, css etc. on index.asp which are relative (i.e. src="images/image.jpg") to reference the paths in their actuall folder, not the rewritten one.

Right now all my page relative images and css breaks because the server is looking for that stuff in a rewritten domain.com/care/ directory that doesn't actually exist.

I'm pretty sure that the apache mod_rewrite rules don't effect relative paths in this way. what's up?

Thanks,

Josh
Feb 21, 2007 at 12:43 PM

janders4 wrote:
Is there a way to use or write your rewrite rules so that they don't break page relative links?
Josh,

This is the way I addressed this issue:

First, add this tag to your ASP pages:

<base href="http://www.domain.com/custom/">

It will tell the browser to use this "base" URL for all relative links.

Next, setup these rewrite rules:

RewriteRule ^/care/([\w\-\%]+)\.(gif|jpg|png|css|js|ico)$ /$1.$2 [I,L]
RewriteRule ^/care[\/]?$ /custom/index.asp [I,L]

The first rule will rewrite the requests for all of your assets (images, css, javascript, etc.). The second does the rewrite for domain.com/care to domain.com/custom/index.asp.

Good luck!

- Bryan
Jul 18, 2007 at 12:50 AM
Thanks csi95, you solved a really aggravating problem for me, too.

I was having a problem with keeping my Theme link good when rewriting as janders4 described. I also found that simply applying the BASE tag with a proper URL suffices to keep relative links together worked to solve the problem; no need for the rewrite rule if you use relative-to-root addressing for image files (e.g., <img src="/images/mypic.jpg" alt="" />).