Forum Moderators: phranque

Message Too Old, No Replies

rewrite rule to link to root directory files

         

willroy

12:16 pm on Jan 17, 2008 (gmt 0)

10+ Year Member



Hi there,

I am using mod rewrite on a php/mysql driven website.

I want to continue using relative linking to files in the root directory i.e. .css and .js files without having to use an absolute link.

I have:

RewriteRule ^(.*\.css)$ http://%{HTTP_HOST}/jbiv8/$1 [L]

The normal address is http://localhost/jbiv8/styles.css

It is not currently finding the stylesheet.

Any suggestions?

With thanks,

Will.

[edited by: jdMorgan at 2:55 pm (utc) on Jan. 18, 2008]
[edit reason] de-linked [/edit]

gergoe

3:41 pm on Jan 17, 2008 (gmt 0)

10+ Year Member



RewriteRule does rewriting (internal redirection) by default, you don't need to define hostname in the replacement pattern in such a case, only absolute or relative path is needed. Note also that
.*
matches everything, with the slashes included, so if you place this into the server config, or your script is in a subdirectory, then you might get undesired results. So I'd suggest to change it as follows:

RewriteRule ([^/]+\.css)$ jbiv8/$1 [L]

Removed the start anchor (^), replaced the . with [^/] (which means anything but slash), replaced the * with + (asterisk: zero or more occurrences; plus sign: one or more occurrences), and finally removed the HTTP_HOST from the replacement pattern.

willroy

4:22 pm on Jan 17, 2008 (gmt 0)

10+ Year Member



Thanks for that. Now all of the styling has been applied, but no url() images have been included?

gergoe

5:39 pm on Jan 17, 2008 (gmt 0)

10+ Year Member



It all depends on the urls you use on the website, you need to know what you want to rewrite to where. If I'm not mistaken when you refer to an url from a css file, then relative paths are evaluated from the directory where the css file is placed (as it looks from the browser), so if you have the url(images/background.gif) in your style sheet, then the browser will look for background.gif in the /jbiv8/images/ directory, not in the /images/ directory.

willroy

1:38 pm on Jan 18, 2008 (gmt 0)

10+ Year Member



yes - my images folder is in the jbiv8 folder

gergoe

2:48 pm on Jan 18, 2008 (gmt 0)

10+ Year Member



And how the urls look like in your css?

willroy

10:50 am on Jan 29, 2008 (gmt 0)

10+ Year Member



They are relatively link i.e. url(images/image.gif)