views:

173

answers:

2

We have the following mod_rewrite condition/rule but it is returning a 404 error on URLs that merely begin "i" (or "css" for that matter) if they do not equate precisely to our corresponding directories (/i /css etc.)

RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|/(i|css|design_and_docs|js|test)/*)
RewriteRule ^(.*)$ /index.php/$1 [L]

This then disallows me from having a route/URI (using CodeIgniter but I don't think that matters) such as /itemid or /idcommand (just a couple I've had need of in the past week)

I can easily modify the RewriteCond as follows to test for a trailing slash but then a request for the /i directory, without the trailing slash, fails whereas it previously worked:

RewriteCond $1 !^(index\.php|robots\.txt|/(i|css|design_and_docs|js|test)\//*)

With the trailing slash /i/ still resolves and that may be good enough for our purposes. But I'd really like /i to still resolve to the corresponding /i directory. So instead of just testing for a trailing slash at the end, I'd like to test for a trailing slash, or the absence of any characters.

How do I do that with a regular expression?

A: 

I'm not sure the / is doing what you're expecting. AFAIK you don't need a leading slash in .htaccess. Have you tried this:

RewriteCond $1 !^(index\.php|robots\.txt|i/|css/|design_and_docs/|js/|test/)
DisgruntledGoat
+1  A: 

Instead of just \/ for the trailing slash, do a subgroup and check for that or the end of string, which is a $: (\/|$)

RewriteCond $1 !^(index\.php|robots\.txt|/(i|css|design_and_docs|js|test)(\/|$)/*)
Amber
Thanks that did the trick. I knew about the $ "anchor" but did not know you could use it in a subgroup.
George Jempty