popomyhobo popomyhobo - 3 months ago 24
PHP Question

Clean URL username redirect

I want

www.example.com/myusername/
to
www.example.com/profile.php?username=myusername
.

Using this code
RewriteRule ^profile/([A-Za-z0-9_]+)/?$ profile.php?username=$1 [L]
I am able to redirect
www.example.com/profile/myusername/
to
www.example.com/profile.php?username=myusername
but how do i do it without including /profile/.

This is my directory index
DirectoryIndex home.php


Which i can always change

Answer

You can use:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9_]+)/?$ profile.php?username=$1 [L]