DriverBoy
DriverBoy

Reputation: 3177

url rewrite and redirect - php

I recently developed a web site using php which accepts a id as its input via get. http://website.com/profile.php?id=123. its the old fashion way, I've seen sites use pretty url's like http://website.com/username.html

So in order to archive this, i managed to do some research and end up with this code blow. Also i'm passing the username in the query string. http://website.com/profile.php?id=123&username=test_user the parameter ID is only used as a input for the profile.php

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST}      /profile.php     [NC]
RewriteCond %{QUERY_STRING}     ^id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^.*                 http://%{HTTP_HOST}/%2.html [R=301,L]

The final output i seek is http://website.com/username.html

Is their anything wrong in this ? this doesn't seem to work. Am i doing something wrong ? if so please be kind to let me know where my error lies. Thank you.

Upvotes: 0

Views: 102

Answers (2)

anubhava
anubhava

Reputation: 786271

You can use this rule:

RewriteEngine On

RewriteCond %{THE_REQUEST} /profile.php\?id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^ /%1/%2.html? [R=301,L]

RewriteRule ^([0-9]+)/([^/.]+)\.html$ profile.php?id=$1&username=$2 [L,QSA,NC]

Upvotes: 1

Selcuk
Selcuk

Reputation: 59444

Try this rule:

RewriteEngine On
RewriteRule ^profile/([^/\.]+)/([^/\.]+).html/?$ profile.php?id=$1&username=$2 [L]

then you should be able to use

http://website.com/profile/123/username.html

as the URL. You can ignore the profile part but I don't recomment it because it would clash with other URLs of your website.

If you only need the username, use

RewriteRule ^profile/([^/\.]+).html/?$ profile.php?username=$2 [L]

and the URL would be

http://website.com/profile/username.html

Upvotes: 1

Related Questions