0

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

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.