Reputation: 302
I have urls in the following format:
http://localhost/profile.php?name=billgates
I have used the following .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?profile/(.*?)/?$ /profile.php?name=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profile\.php\?name=([^\&\ ]+)
RewriteRule ^/?profile\.php$ /profile/%1? [L,R=301]
Which converts urls to the format:
http://localhost/profile/billgates
Please let me know how I can edit the .htacces file to remove the profile/ and make urls in the format:
http://localhost/billgates
Upvotes: 0
Views: 34
Reputation: 45829
You'll need to be as restrictive as possible with the regex in order to avoid conflicts. For example, you need to be able to differentiate between /<some-name>
and /<some-page>
(if that is a thing on your site).
I'll assume that names can only consist of lowercase letters, as per your example.
You should also have the external redirect before the internal rewrite.
Try the following instead:
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profile\.php\?name=([^\&\ ]+)
RewriteRule ^profile\.php$ /%1 [QSD,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+?)/?$ /profile.php?name=$1 [L]
Since the regex is now more restrictive (only lowercase letters) the filesystem check that checks that the request does not match to a file can be removed (files in the document root are unlikely only going to consist of lowercase letters).
Upvotes: 1