Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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'm setting up a website that (ideally) would allow users to access other users' homepages with a url in the format "" where 'ThisLanham' is the username. The username begins with a letter and can consists of any alphanumeric character along with an underscore, hyphen, or period.

So far, the redirection has worked perfectly when I ignore usage of the period character. The following code handles that request:

RewriteRule ^([a-zA-Z][0-9a-zA-Z-_]*)/?$ Page/?un=$1 [NC,L]

However, I've tried a number of ways it check for the period as well, but all have resulted in a 500 Internal Server Error. Here are some my attempts:

RewriteRule ^([a-zA-Z][0-9a-zA-Z-\_\\.]\*)/?$ Page/?un=$1 [NC,L]
RewriteRule ^([0-9a-zA-Z-\_\\.]\*)/?$ Page/?un=$1 [NC,L]
RewriteRule ^([a-zA-Z].\*)/?$ Page/?un=$1 [NC,L]
RewriteRule ^(.\*)/?$ Page/?un=$1 [NC,L]

also tried...

RewriteCond $1 != index.php
RewriteRule ^([a-z][0-9a-z-_.]*)/?$ Page/?un=$1 [NC,L]

My backup plan is to no longer allow users to include periods in their usernames, but I'd much rather find a solution. Any ideas?

share|improve this question

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .?{REQUEST_URI} [R=301,L]

Here, to denote that {HTTP_HOST} is an Apache variable, we must prepend a % character to it. The regex begins with the ! character, which will cause the condition to be true if it doesn't match the pattern. We also have to escape the dot character so that it matches a literal dot and not any character, as is the case with the dot metacharacter. We've also added the No Case flag to make this operation case-insensitive.

Maybe this helps ?

share|improve this answer

According to the documentation for Perl's regular expressions (which is what Apache uses),

RewriteRule ^([a-zA-Z][0-9a-zA-Z\-_.]*)/?$ Page/?un=$1 [NC,L]


RewriteRule ^([a-zA-Z][0-9a-zA-Z_.-]*)/?$ Page/?un=$1 [NC,L]

should work. The period is treated as a real period, not a metacharacter, when it's part of a character class (i.e. between brackets [ and ]), so in this case you shouldn't need to escape it.

The hyphen, on the other hand, does have a special meaning within a character class (unless it's the first or last character in the class, cf. second example above).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.