0
votes

Each user can visit their account profile by www.example.com/profile/{username}

note: username contains numbers and letters only

For example: www.example.com/profile/michaelwatson www.example.com/profile/jenniferlee2

Would like to redirect from old URL: www.example.com/profile/jenniferlee2

to the new shorter URL www.example.com/jenniferlee2

I want anyone that have old bookmarks to go from old URL to the new URL and anyone looking but only for profile URLs. Anything else should remain the same.

This is what I tried adding to .htaccess but getting internal server error

RewriteRule (.*) /profile/$1 [L]

1
And how do you know a profile is not a profile with the new URL from the OLD one? For instance if I access http://www.example.com/contact and that is handled by index.php I will get redirected to /profile/contact given your current rule. - Prix

1 Answers

0
votes

You can use this code in root .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+profile/(\w+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/profile/
RewriteRule ^(\w+)$ /profile/$1 [L]