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!
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]
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With