0
votes

I am trying to set up URL like www.example.com/username for something like www.example.com/profile.php?name=username

I tried this in my .htaccess

    RewriteEngine on
    RewriteRule ^user/(.*)$ profile.php?name=$1 [NC,B,QSA]

but this makes it work for all the pages. I want this to work only when URL is like www.example.com/username but it works also for www.example.com/users.php

If users.php or any other .php file is opened then the rewrite rule should not be applicable to it.

How can this be achieved?

1

1 Answers

0
votes

You can add a rewrite condition on the request uri which matches everything not ending in .php

e.g.: (not tested)

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^user/(.*)$ profile.php?name=$1 [NC,B,QSA]

checking if the uri is actually a file was very popular with frameworks, but it is slower than the string match ending with .php because every request apache needs to check if the file exists on disk.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/(.*)$ profile.php?name=$1 [NC,B,QSA]