Take the 2-minute tour ×

In my .htaccess file, I've got the fairly standard

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1 [NC]

and I only need this to happen to files in my root directory. However, if I go to the URL of one of my subdirectories (with or without a index.php file), such as www.foo.com/bar, then I am redirected to www.foo.com/bar/?p=bar - how do I prevent the addition of ?p=bar?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You can try making the rule only execute for non directories as below

#if it is not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#then send it to index.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1 [NC,L]
share|improve this answer

You can put .htaccess files into the sub-directories.

# /bar/.htaccess

AllowOverride none

This should prevent the .htaccess inheritance.

share|improve this answer
I am not sure, if this is the best approach; he'd have to update all subdirectories ad infinitum to excape his overeager rewrite rule on root level. –  kontur Jan 9 '12 at 13:46
Well, yes. Another option is to add checks for each sub-directory in a RewriteCond, but that, also, would have to be maintained for every new directory. But to your point, that is probably more reasonable... –  clmarquart Jan 9 '12 at 14:00

Your Answer


