Pathik Gandhi
Pathik Gandhi

Reputation: 1344

Want to create Rewriterule for username in .htaccess?

i m creating a website in that site you can access you profile with entering your Username in the url. like http://www.example.com/username

i have created the .htaccess file and add the rewriterule for the username. See below

RewriteRule ^([_A-Z0-9a-z-+]+)$ index.php?p=home&username=$1 [S=1]
RewriteRule ^([_A-Z0-9a-z-+]+)/$ index.php?p=home&username=$1 [S=1]

It works perfectly when enter the username in the url. but when i add the username including the . operator it will not works. See below

Works with this
http://www.example.com/username

But

Not works with this
http://www.example.com/user.name

Can any body help me what to change in my rewriterule to working with dot(.) operator.

Upvotes: 0

Views: 904

Answers (1)

Jon Lin
Jon Lin

Reputation: 143896

You need to change the ([_A-Z0-9a-z-+]+) to include matching against the dot: ([_A-Z0-9a-z-+\.]+)

EDIT:

Add RewriteCond before the rule:

RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^([_A-Z0-9a-z-+\.]+)/?$ index.php?p=home&username=$1 [L]

Or alternatively, to avoid rewriting other PHP files or files that exist, try this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([_A-Z0-9a-z-+\.]+)/?$ index.php?p=home&username=$1 [L]

Upvotes: 2

Related Questions