How to use .htaccess to rewrite URL's with @username

Viewed 214

I have a profile system in PHP. If you want to access their profile, you have to goto /profile.php?user=username I want to be able to rewrite this /@username.

Currently, my .htaccess code is this:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ - [F]
    RewriteRule ^@(.*)$ profile.php?user=$1 [L,QSA]

FIXED: I had to change AllowOverride to All in my apache configuration.

<Directory /path/to/directory>
   AllowOverride All
</Directory>

and changed my htaccess file to: (thanks to the person who answered this question)

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^@(.*)/?$ profile.php?user=$1 [L]
1 Answers

As per your shown samples could you please try following. Where considering that you are hitting URL like http://localhost:80/@test" which you want it to be served by http://localhost:80/profile.php?user=$1

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^@(.*)/?$ profile.php?user=$1 [L]

Also as mentioned in comments you don't want to use [F] rule of yours shown sample htaccess so I had removed if from here.

Related