Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

i need to set up an apache RewriteRule in order to have this behavior:

example.com/foo => index.php?module=foo&action=&args=
example.com/foo/bar => index.php?module=foo&action=bar&args=
example.com/foo/bar/baz => index.php?module=foo&action=bar&args=baz
example.com/foo/bar/baz/foobaz => index.php?module=foo&action=bar&args=baz/foobaz

I wroted this regex:

RewriteRule ^(.[^/]*)(\/)?(.[^/]+)?(\/)?(.+)?$ index.php?module=$1&action=$3&args=$5 [L,QSA]

It works well, i only need to tell apache to ignore all urls that end with some specific estension (for example, .jpg, .png, .css, .js, .gif, etc..); Those should be treated as normal, e.g. example.com/css/style.css should show the CSS file (is it exists).

Any idea?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

An easy way to get around it is this

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.[^/]*)(\/)?(.[^/]+)?(\/)?(.+)?$ index.php?module=$1&action=$3&args=$5 [L,QSA]

That tells apache to not run the rule if the file is a real file on the system that can be served up.. Now you if you want to do it for just image/css/js files you could probably do

RewriteCond %{REQUEST_FILENAME} !^(.*).(js|jpg|css|gif)$
RewriteRule ^(.[^/]*)(\/)?(.[^/]+)?(\/)?(.+)?$ index.php?module=$1&action=$3&args=$5 [L,QSA]

That one is untested but you get the idea.

share|improve this answer
    
+1 ... but the second RewriteCond regexp would be more precise if the period preceding the file extension were escaped. –  danlefree Nov 30 '10 at 12:04
    
Im already using the first rule; In fact in my case the problems come when files doesnt exists! I'll try the second regex! –  Strae Nov 30 '10 at 13:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.