Hardik
Hardik

Reputation: 1283

Remove query string and make clean url using htaccess

I have url's like following with query string. I want to remove query string from url and want to make clean url.

www.demo.com/following.php?user=hardik
www.demo.com/fans.php?user=john

This url's should be like

www.demo.com/hardik/following
www.demo.com/john/fans

OR should be like

www.demo.com/following/hardik
www.demo.com/fans/john

Is this possible with htaccess? I tried to find a loot in google but still no luck. Need help.

Update:

I need something like this

www.demo.com/user/hardik/following/
www.demo.com/user/john/fans/

I tried like this

RewriteRule ^user/([^?]*) following.php?user=$2/ [L,QSA]

Upvotes: 1

Views: 1231

Answers (3)

Hardik
Hardik

Reputation: 1283

Finally this is working for me for my last requirement. May be help someone else.

RewriteEngine On
RewriteBase /

RewriteRule ^user/([^*]+)/following/$ following.php?user=$1 [L,QSA]
RewriteRule ^user/([^*]+)/fans/$ fans.php?user=$1 [L,QSA]

Upvotes: 0

anubhava
anubhava

Reputation: 784998

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

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(\w+)/(\w+)/?$ $2.php?user=$1 [L,QSA,NC]

Upvotes: 1

Castillo
Castillo

Reputation: 145

Try with this for a url like www.demo.com/following/hardik

Options +FollowSymLinks

RewriteEngine on

RewriteRule ^([a-zA-Z_-]+)/([a-zA-Z_-]+)/?$ $1.php?user=$2

Upvotes: 0

Related Questions