Rewriting URLs for users

Can I hijack this for a second, because I understand how that rewrite works and it seems like it might be close to what I want

I want to make it so that whenever someone types in www.mysite.com/user, the page loaded is www.mysite.com/user.php?user=user

Is it simple to change the ^(signup|login|blog)$ to achieve this? Or is that going to be a bit tricky. I have googled a little bit but can’t find information (yet) on that specifically.

Thanks for any tips/pointers in the right direction… :slight_smile:

It’s not very handy to use the a|b|c|d scheme here because then you would have to add each user to rule once he signs up, which doesn’t sound like a very good idea, does it? :slight_smile:

What you need instead is wildcards. Assuming the usernames can only contain letters and digits, you’d use


Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)$ user.php?user=$1 [L]

Which means; if the request is not for an existing directory (as per the first RewriteCond) and it’s not an existing file (as per the second RewriteCond) and it only consists of letters and digits, rewrite to user.php (as per the RewriteRule).

:slight_smile:

Awesome, makes good sense to me and works great, but I’m curious what the Options line does?

I assume I can plug my own username regex into it as well, such as


RewriteRule ^([a-zA-Z0-9_-]{5,30})$ user.php?user=$1 [L]

My username regex in php (that the rewrite one will be copied from) also ended in /i, but I’m not sure why… and it didn’t work when I put it in the rewrite… so I just removed it…

Seems to work…

The options line does two things

  1. Enable FollowSymLinks - Make Apache follow symbolical links (on *nix machines), this is a requirement of mod_rewrite. If this is not enabled mod_rewrite won’t work. Most hosts have it enabled, but it doesn’t hurt to enable it again (which is also fool proof in case the hosts decide to disable it at some point in the future)
  2. Disable MultiViews - A system in Apache that serves fallback files when the request doesn’t exist. For example if one requests /images and there is no directory by that name but there is a file images.php it will serve that instead. It’s basically just a stupid system that makes stuff very unpredictable so I always advise people to turn it of just in case.

As for the regular expression, you can indeed replace the one I provided with your own. The /i at end looks like either PHP or javascript regex, where the i stands for case insensitive. Since you’re using a-zA-Z in the class range definition you don’t need that in the regex (since a-zA-Z makes it case insensitive all by itself without the need of any flags), which is why it works without it (and you can also remove it in the php/javascript).

Thank you very much for the explanations.

That is all actually very easy to understand and implement… thanks so much!

woo