Using mod_rewrite


I’ve been at this for a few hours now, and I can’t seem to find anything that works. I’ve tried combinations of things and nothing seems to work out.

What I am trying to do is when a user visits:

it will rewrite to: (or even /index.php?u=xyz)

“user” will be dynamic, so it can be user, person, 3xyzn …etc

Now, I don’t want it to rewrite if the file/directory actually exists. Any ideas on how to make this work?

I’ve tried this:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} ^/(.)$
RewriteRule ^/(.
)$ /index.php?u=$1 [R,L]

It keeps giving me a 404. Any ideas?


Read about the get statement in the php manual.
You will find that a form even if it is a hidden input type will create a url with the ?name of varable=value in it. So a rewrite is not really required. But then again I am sure someone could think of a reason that I didn’t.
The manual is at


Not sure if I understand your response, or my initial question was not too clear.

Basically, if I typed in: and it did not exist on the server, I would be given a 404 error. What I would like to do is to use mod_rewrite to take that and just redirect it to: /index.php?u=dir

So what I would like to happen is this:

  • If the URI is not in the “file.ext” form (ala directory) and does not exist on the server, rewrite the url.

  • If a directory/file does exist on the server, do not rewrite. (if I had a subdirectory called “gallery” and I typed the rewrite would be called because I didn’t specify /gallery/index.php)

  • If a “file.ext” was requested and is not on the server, produce a 404 error (request and it does not exist, produce a 404 error)

I have updated my code a bit and have some success, but it still fails if the URI is not a “file.ext” format (e.g directory)

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} ![a-z]+.[a-z]+
RewriteRule ^(.*)$ /index.php?u=$1 [L,NC]


Got the answer for those who would be interested:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ![a-z]+.[a-z]+
RewriteRule ^(.*)$ /?var=$1 [L,NC]

The key was to use REQUEST_FILENAME and not REQUEST_URI when doing a check to see if file/directory exists.


Interesting, yep I misunderstood you. I thought you were trying to make a static page dynamic.
I got aways to go yet on learning rewrites.


Glad you got it worked out.

If you didn’t find this while you were searching for your solution, it could come in handy in the future:

:stuck_out_tongue: Save up to $96 at Dreamhost with the 96DOLLARSOFF promo code.


Thank you for that cheat sheet, that should help a lot in learning rewrite rules. One question would you happen to know why when I use a rule to block images from referrers, that it blocks my site.
Here is the condition for my site:
RewriteCond %{HTTP_REFERER} !^http://(www.)?*$ [NC]

BTW, I want to place this in a sub directory so only some images are blocked.


Just thought of something, I am using a firewall on my system, would that block the referer header? Only reason I ask is I know I need to disable the firewall in order for W3C’s validater to work from a link.


Interesting, I got it to work. As long as I reaccess the page via url it works, just when doing a page refresh it does not work. The firewall does not effect it either btw.