Reputation: 135
I have a .htaccess file on a website I'm working on which rewrites urls from mydomain.com/sub/folder/ to mydomain.com?index.php?controller=sub&view=folder
Unfortunately the way I've written it means I can't access images, stylesheets and other linked files anymore. Could anyone tell me how best to exclude specific directories / URL requests from the rewrite rule?
Apologies if this is a bit of a newbie question, I'm still wrapping my head around this mod rewrite stuff!
The .htaccess file looks like this:
RewriteEngine on RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?Controller=$1 RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-_]+)/? index.php?Controller=$1&View=$2
Upvotes: 0
Views: 888
Reputation: 143956
If your images are in mydomain.com/images
and you are linking to them using relative links on the page mydomain.com/sub/folder/
the browser is going to try to attempt to access the image via mydomain.com/sub/folder/images/i.gif
. But if you change your links to absolute links, the browser will correctly attempt to load mydomain.com/images/i.gif
. However, the RewriteRule will change it to: mydomain.com/index/php?Controller=images&View=i.gif
. To avoid this you need to add a few RewriteConds:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]+)\/?$ index.php?Controller=$1
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)\/? index.php?Controller=$1&View=$2
So that when attempting at access an existing file/directory, don't rewrite to index.php.
Upvotes: 1