0

I'm struggling with a mod_rewrite problem that involves .css files that are dynamically served by a "program" that reference images. I say "program" since the the tech stack is a bit obscure, but just insert your favorite technology there [perl | php | java].

The CSS file is referenced from the page using the following URL:

http://server/dir/subdir/file-program?site-id=1234&fname=stylesheet.css

The content of the CSS include image references like the following:

background-image:url('hello.png'); 
... 
background-image:url('world.png');

The browser interprets these references as /dir/subdir/*.png.

What I want to do is rewrite all of those requests using the URL of the referrer so they end up looking like:

http://server/dir/subdir/file-program?site-id=1234&fname=hello.png
http://server/dir/subdir/file-program?site-id=1234&fname=world.png

Note that the site-id is variable, so I would really need to reference the referrer to pick up the whole query string. Sorry I left this out of the original post.

Put differently: Any image references that comes from ...file-program...*.css should be rewritten to use the URL (including query string) of that CSS, just substitute the image file name for the css file name.

Any help would be greatly appreciated.

2
  • Can't you just fix file-program to generate URLs like that? Commented Dec 2, 2009 at 17:17
  • Dominic, I wish it were that easy. Unfortunately "file-program" is not open-source or something I control. It might change in future versions of the product, but for now I think mod_rewrite is the quickest way to resolve it. Commented Dec 2, 2009 at 17:20

1 Answer 1

1

I can't make any promises, but give this a shot:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s [OR]
RewriteCond %{REQUEST_FILENAME} !-l [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/dir/subdir/(*.)$ /dir/subdir/file-program?fname=$1&referer=%{HTTP_REFERER}
RewriteRule ^/dir/subdir/file-program?fname=(.*?)&referer=(*.)siteid=([0-9]+) /dir/subdir/file-program?fname=$1&siteid=$3
Sign up to request clarification or add additional context in comments.

1 Comment

Jordon, Looks like I left out a key detail and that is that the site-id is variable, so I would like to reference the referrer (the .css file) to pick up the query string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.