Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 996
Liam Avatar asked Nov 04 '22 21:11

Liam


2 Answers

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]
like image 92
Gerben Avatar answered Nov 09 '22 08:11

Gerben


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

like image 38
Niet the Dark Absol Avatar answered Nov 09 '22 07:11

Niet the Dark Absol