Liam
Liam

Reputation: 85

escape period (.) character in mod_rewrite regex

I currently have a .htaccess file set up with this rule:

RewriteRule ^([a-zA-Z0-9_-]+)$ user\.php?user=$1 [L]

which works as expected: mysite.com/joe becomes mysite.com/user.php?user=joe

How could I change the regex to include periods in a possible username?

For example I would like mysite.com/joe.bloggs to go to mysite.com/user=joe.bloggs

Currently this brings up a 404 missing page error.

I have tried ^([a-zA-Z0-9\._-]+)$ user\.php?user=$1 [L] But this produces an internal error 500 (I think this is due to an infinite loop: user.php is designed to redirect to the homepage if no user is specified.)

I'm pretty new to all of this (regex especially). Thanks for any help!

Upvotes: 5

Views: 4383

Answers (2)

Gerben
Gerben

Reputation: 16825

First check if the requested URLs is not a file or folder. This will prevent the redirect loop for URLs like /user.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-.]+)$ user.php?user=$1 [L,QSA]

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

. has no special meaning inside a character class, so there is no need to escape it.

If that doesn't help, try putting die("Test") right at the start of user.php. This will allow you to see if the error is coming from the rewrite, or the PHP file.

Upvotes: 1

Related Questions