hsn0331
hsn0331

Reputation: 394

htaccess - rewriterule help needed

I am having problem with my rewrite rules. I have some urls with variables which look like this: localhost/mysite/user.php?u=username. I want to convert them to this: localhost/mysite/user/username/

This is what I have tried

RewriteRule  ^user/([A-Za-z0-9-]+)/?$    /mysite/user.php?u=$1    [NC,L]    
# Handle user page requests

but its not working :(

/mysite/user.php is in htdocs folder (because I am in local environment)

If somehow url like this opened localhost/user.php?u=username it should redirect or change to localhost/user/username/, is this possible?

Upvotes: 1

Views: 34

Answers (1)

anubhava
anubhava

Reputation: 784918

You can use this code in your /mysite/.htaccess file:

Options -MultiViews
RewriteEngine On
RewriteBase /mysite/

RewriteCond %{THE_REQUEST} /user\.php\?u=([^\s&]+) [NC]
RewriteRule ^ user/%1/? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

RewriteRule ^user/([^/.]+)/$ user.php?u=$1 [L,QSA,NC]

Upvotes: 1

Related Questions