views:

359

answers:

2

Hey,

I have a problem with the regular expression at the bottom. I'm trying to pass all patterns except index.php, robots.txt and any css and js file through the index file, however I have one specific path where I need to process js and css files through php. Normally my url structure is like this:

example.com/class/function

lets say I want to process css and js files from the below pattern:

example.com/view/file/xxxxx.js/css

normally I would have thought this code would work but it doesn't:

(.*)(?!view\/file)(.*)\.(js|css)

for some odd reasons this code work (removed 'view' and added \/ before (.*)):

(\w*)(?!\/file\/)\/(.*)\.(js|css)

but then I won't be able to load files from the main directory:

example.com/file.js

And when I modify the file like this, it doesn't work again... (all I did was to make \/ optional):

(\w*)(?!\/file\/)(\/?)(.*)\.(js|css)

Here is the original htaccess file:

RewriteCond $1 !^(index\.php|robots\.txt|(.*)\.(js|css)) 
RewriteRule ^(.*)$ /index.php/$1 [L]
+1  A: 

Assuming I have understood correctly, something like this should work:

#rewrite specific css/js
RewriteRule ^view/file/(.*)\.(css|js)$ file.php?file=$1.$2 [L]

#only rewrite anything else if the path is not an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#rewrite to main handler
RewriteRule ^(.*)$ index.php/$1 [L]
Tom Haigh
I didn't think about separating them, but shouldn't (?!view/file) work if I want to combine them?
Dennis
Dennis, I think Tom Haigh's idea (and mine) dispense with the negative lookahead because it is confusing and hard to tell if it "should" work. In mod_rewrite, it's sometimes easier to define what you *do* want to match and what it should be rewritten to.
bmb
+1  A: 

For readability and debuggability, I recommend you separate some of your rules.

# Rewrite the special case
RewriteRule ^view/file/(rest of rule)

# Skip these
RewriteRule ^index\.php$ - [L]
RewriteRule ^robots\.txt$ - [L]
RewriteRule ^(.*)\.(js|css)$ - [L]

# Main rule
RewriteRule ^(.*)$ /index.php/$1 [L]
bmb