How to allow a dot in url htaccess?

I'm starting out with php and I want to use a dot (.) in url. For example someone with username test.test1 should point me out to a profile of a user with that username but I get an error message saying object not found. However I if I use letters,numbers, dash or underscore it works just fine.

Here it is working perfectly fine

But this is what happens when I use a dot in url: enter image description here

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?profile_username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?profile_username=$1

This is all the .htaccess code I have.

1 answer

  • answered 2019-07-10 22:58 Hadi77

    Your Regular Expression doesn't match dots.

    The sequence of characters between brackets mean "Match only these characters once", and the + out of it means "Match more than one times". So only characters defined between brackets are matched in the requested URL, which won't contain dots. You should add the "dot character" in your sequence of characters, but you should also keep in mind that a dot has the meaning of "every character found" in regex, so you should escape it with a backslash, like this: \.

    So your regex from ^([a-zA-Z0-9_-]+)$ should become ^([a-zA-Z0-9_-]+\.)$

    RewriteEngine On
    
    RewriteRule ^([a-zA-Z0-9_-\.]+)$ profile.php?profile_username=$1
    RewriteRule ^([a-zA-Z0-9_-\.]+)/$ profile.php?profile_username=$1
    

    P.S: I recommend you read some articles about Regular Expressions and use a "Regex Tester" to learn about regular expressions. For example, this site is a good starting point, and this is a good online tester.