Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a few rewrite rules like these

RewriteRule ^dir/(.*)-something.html otherdir/file1.php?var1=val&var2=$1 [L,NC]
RewriteRule ^dir/ otherdir/file2.php?var1=val [L,NC]

dir/ is not a real directory. Everything above works as expected.

However, the user is able to type anything like

And they are still redirected to file2.php. If the user types in just garbage I'd like to serve a 404 instead.

I'm guessing I need a RewiteCond that will test for blank space after the slash and only then serve file2.php, but I'm unsure how to write it.

share|improve this question
up vote 3 down vote accepted

I think if you put a $ at the end of dir/ it will do the trick (note that I haven't tested this, so please try it out before voting me up or anything). The $ means "end of string" in the same way that ^ means "beginning of string".

RewriteRule ^dir/$ otherdir/file2.php?var1=val [L,NC]

Good luck!


share|improve this answer
Worked like a charm! Thanks! I can't vote up yet (reputation) or I would. – Stephen Apr 1 '10 at 0:04
Also made me realize that I needed a $ after the .html in the other rules. Appreciate it! – Stephen Apr 1 '10 at 0:11

Your Answer


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.