Forum Moderators: phranque
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
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]
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)$
Jim