Htaccess rewrite

Greetings all!

Let me describe the situation first.

I have a subdomain (sub.domain.tld) which has a few folders, each of them containing a website.

I would like to be able to specify a rule in .htaccess that would act like this

if referring path filename is CSS type
rewrite files to /path/[…]

Let me give out an example:

style.css has a style { background:url(/image.png); }

image.png is not in sub.domain.tld/image.png but rather in sub.domain.tld/path/image.png

So, if the referring file is of CSS type add an extra level to the path.

What I got so far is:

RewriteCond %{HTTP_REFERER} \.css$
RewriteRule (.*) path/$1

which works for one directory at a time (I have to manualy change “path” to “path2” and so on). I can’t seem to figure out how to make a variable out of “path”.

I think it would be something like

RewriteCond %{HTTP_REFERER} \.css$
RewriteRule (.*) substr(0,5,%{HTTP_REFERER)/$1

but of course that’s not going to work.

Actually, the tricky part is that I can’t use a REGEX on the second part of the RewriteRule. If I could I would just extract the “path” part from the referer and add it to the path of the file being asked for.

Thank you to anyone whom is considering the time to help me find a solution.

I seem to have solved my problem.

RewriteCond %{http_referer} \.css$
RewriteCond %{http_referer} .ro/(.+)/.+\.css$ [NC]
RewriteRule .* %1%{request_uri}

(ro being the tld)

I hope I won’t run in any more problems …

Hopefully this may be of some assistance to someone someday …

bsorin,

Good explanation of the problem and a seemingly good solution. Let’s go through that, though.

My first concern is over the {HTTP_REFERER}: Is it the URL preceding the requested script or the css? I would have thought it was the URL - although I have to run a test to verify that.

Your solution:

RewriteCond %{HTTP_REFERER} \.css$ - excellent (provided that I’m incorrect in my assumption above)

RewriteCond %{HTTP_REFERER} .ro/(.+)/.+\.css [NC] - if a Romanian domain, be sure to specify that by escaping that lead dot in the regex; (.+) is the (URI) path which belongs to the {HTTP_REFERER}. If that path (and {HTTP_REFERER}) are correct, then the css and No Case flag are good.

RewriteRule .* %$1%REQUEST_URI} are fine as far as they go BUT .* should be .? (meaning anything, not lotza everything/nothing), the path as above and the {REQUEST_URI} IS the way to go! Unfortunately, you omitted the Last flag which will be CRITICAL if any mod_rewrite statement follow. Use the Last flag as you would } for a PHP block or ; to terminate a PHP statement.

PLEASE let us know whether this {HTTP_REFERER} works for your problem.

Regards,

DK

Hi David!

Thanks for taking the time to respond to my post.

According to www-askapache-com/htaccess/mod_rewrite-variables-cheatsheet.html

HTTP_REFERER should be giving out something like
www-askapache-com/pro/mod_rewrite/catch.php?k=i

The final code I’m using (after I read your post) is

RewriteCond %{http_referer} \.css$
RewriteCond %{http_referer} \.ro/(.+)/.+\.css$ [NC]
RewriteRule .?$ %1%{request_uri}

I hope I understood your points correctly.

However there seems to be an even bigger problem. This works fine on mozilla browsers (firefox(win+mac - v2.0 through v3.5) and camino(mac)), but for the rest of the browsers (ie v6 to v8, safari v3 &v4, chrome v3) it doesn’t work.

May it be possible that all these browsers don’t give out the http_referer variable?

I user browserlab. adobe. com to test my designs.

The page I am talking about would be clienti.bsorin.ro, with all it’s subdomains being a different site (work in progress).

Thanks again!

Regards,
Sorin

Sorry for having to break the links, I couldn’t post otherwise.

bsorin,

No worries about the time nor the links - understandable (and the SitePoint restriction there is to deter SPAMmers).

Cheatcheet? :eek2: Horrors! Ever consider reading the tutorial linked in my signature?

Okay, I just tested (locally) and received (from a PHP script): [B]http://test/Test_referer1.php[/B] for that script (linked to itself in FF). I did not have the time to futz with the css the way that you were suggesting.

Actually, my code would NOT have the $ in the regex of the RewriteRule, a Last flag IS required (if there is subsequent code - but it’s GOOD programming technique to ALWAYS terminate the block statement) and I believe that a / MAY be required to separate the path from the {REQUEST_URI}:

RewriteCond %{http_referer} \\.css$
RewriteCond %{http_referer} \\.ro/(.+)/.+\\.css$ [NC]
RewriteRule .? %/1%{request_uri} [L]

I had prefaced my last post with my disbelief that the {HTTP_REFERER} would change for the subrequests for a URI’s support files. It seems that you are hitting that problem with the non-compliant (DOM) browsers.

If you’re using a subdomain to fetch the css files, though, WHY do you have to go through this (to fetch the css file from that subdomain’s DocumentRoot - or css subdirectory)? Using the same location within each domain would (seemingly) solve that problem for you. Okay, just thinking of the REAL problem, not the stated problem (use mod_rewrite to read the {HTTP_REFERER} et al).

Regards,

DK

Tutorial? :ehhh2: It’s a great tutorial but there are hundreds like it. No, actually there are thousands like it. As far as I am aware, there is no cheatsheet like the above referenced… (that came from the ground-breaking askapache-com=htaccess+crazy-advanced-mod_rewrite-tutorial experiment) It looks simple but that’s the point, I have it printed out and use it almost every day. I imagine it wouldn’t be so busy around if more people knew aboutit… and you know sitepoint makes its money off this.