1

I'm setting up a website that (ideally) would allow users to access other users' homepages with a url in the format "www.mysite.com/ThisLanham" 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?

2 Answers2

1

http://articles.sitepoint.com/article/apache-mod_rewrite-examples

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example.com%{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 ?

1

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]

or

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).

David Z
  • 5,475
  • 2
  • 25
  • 22