Forum Moderators: phranque

Message Too Old, No Replies

Path resolving problems (js/css/images)

         

Admitone

5:53 pm on Sep 9, 2009 (gmt 0)

10+ Year Member



Hello,

I am sure this question/issue has been resolved many times before, but I wasn't able to find a clear answer. So here's my problem.

Obvious, I am using mod_rewrite to get clean urls, everything is fine, except images and other assets.

here's a part of my htacces:


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(.+)/.css$
RewriteCond %{REQUEST_FILENAME} !^(.+)/.js$
RewriteCond %{REQUEST_FILENAME} !^(.+)templates/.php$
RewriteRule . index.php [L]

What I do, I redirect everything to index.php, then in index.php I split the url (example.com/my/clean/url) and place it into an array. This method works like a charm, but here's the problem:

Everything works when it's like example.com/home, but when I add a traling slash, apache thinks it's a new directory, so my stylesheets paths aren't resolving.

example.com/home <-- WORKS
example.com/home/(even more) dosn't reslove

Note that i've referenced the stylesheets in the header like cssfolder/main.css, so by going to example.com/home/
apache will think it's a new folder and will search in it.

Sorry for the long message, I tried to be as explicit as I could.

Thank you,
Rob

jdMorgan

6:59 pm on Sep 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The correct solution is to use hostname-relative linking with a link to /cssfolder/main.css instead of using page-relative linking.

Realize that it is the browser that resolves relative links. If you add a 'virtual directory' path element into the URL (e.g. as a way of passing query parameters), then the browser will also use that additional path-element when resolving relative links on your pages to the full URLs that it must use to include those objects.

You will likely be *much* happier with the performance of your code if you move your file-exists checking RewriteConds to last and exclude as many filepaths and filetypes as possible, to prevent your server from having to go read your disk twice for each and every HTTP request, and make a few additional minor tweaks to eliminate unnecessary/wasteful regex subpatterns and anchoring as well:


RewriteEngine on
#
RewriteCond %{REQUEST_FILENAME} !\.css$
RewriteCond %{REQUEST_FILENAME} !\.js$
RewriteCond %{REQUEST_FILENAME} !.templates\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Do include exclusions for other filetypes as well, such as .gif, .jpe?g, .png, .txt, .xml, etc. if applicable.

You could check the URI instead of the filename (slightly-faster), and combine all of these filetype exclusions into one line, such as


RewriteCond %{REQUEST_URI} !\.(css¦js¦gif¦jpe?g¦png¦txt¦xml)$

but be aware that you must change the broken pipe "¦" characters to solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

Admitone

7:18 pm on Sep 9, 2009 (gmt 0)

10+ Year Member



Thank you for the tips, I'll try to apply them and see what I get ;)

Oh, btw, as is pretty late here, by hostname relative linking you mean (http://example.com/cssfolder/main.css)?

I'll get with updates ASAP

UPDATE: Thank you very much, worked like a charm ;)

jdMorgan

4:01 am on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, I meant linking with a leading slash, as shown in the first sentence of my reply. You can use a full URL if you wish -- either method should solve the problem.

Jim