views:

40

answers:

2

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/Page/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 ^page/([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 ^page/([a-zA-Z][0-9a-zA-Z-\_\\.]\*)/?$ Page/?un=$1 [NC,L]
RewriteRule ^page/([0-9a-zA-Z-\_\\.]\*)/?$ Page/?un=$1 [NC,L]
RewriteRule ^page/([a-zA-Z].\*)/?$ Page/?un=$1 [NC,L]
RewriteRule ^page/(.\*)/?$ 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???

A: 

The period isn't a metacharacter within a (bracketed) character class. The hyphen, however, is. [Z-_] is the range of characters from Z to _. In ASCII, this is equivalent to [Z\[\\\]^_]. Try:

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

Note: since comparison is case-insensitive, I didn't bother with the A-Z character ranges.

As far as rewriting is concerned, you might want to redirect all URLs beginning with "Page" and handle illegal usernames elsewhere, such as an "Unknown User" page or having your script (rather than the server) generate a 404.

RewriteRule ^/?page/([^/]+)/?$ Page/?un=$1 [NC,L]

Should you later decide to expand on valid usernames, this approach will make for an easier transition. Since you'll need to handle unknown but valid usernames in your scripts, the second approach shouldn't require any extra work.

outis
A: 

The problem is probably that the destination you are redirecting to is also matched by this rule and thus leads to an infinite recursion.

Try this rule that excludes the destination (in this case index.php):

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