Farasat Ali
Farasat Ali

Reputation: 35

URL Rewrite Rules in specific format

I have written a .htaccess file for URL to rewrite but it has some error due to which does't give desired output, need some expert opinion/result to get it right.

Here is URL I want to rewrite

http://theyouthtalent.com/user-wall.php?User=dillagiary&Talent=T.V

should be rewrite to

http://theyouthtalent.com/T.V/dillagiary

and my existing .htaccess file has following lines

RewriteEngine On

RewriteCond %{THE_REQUEST} /user_wall\.php [NC]
RewriteCond %{QUERY_STRING} ^User=(\w+)&Talent=(\w+)$ [NC]
RewriteRule ^user_wall\.php$ /%2/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/(\w+)/?$ user_wall.php?User=$2&Talent=$1 [L,QSA]

Important point is I want to Talent value first and then username

Upvotes: 2

Views: 135

Answers (1)

MrWhite
MrWhite

Reputation: 45988

http://theyouthtalent.com/user-wall.php?User=dillagiary&Talent=T.V

The Talent query string parameter value contains a dot (.), but the shorthand character class \w (word characters) does not include a dot. So, you need to include a dot in the pattern. ie. Change the relevant (\w+) pattern to ([\w.]+).

Also, the request URL contains a hyphen (-), not an underscore (_).

For example:

RewriteCond %{THE_REQUEST} /user-wall\.php [NC]
RewriteCond %{QUERY_STRING} ^User=(\w+)&Talent=([\w.]+)$ [NC]
RewriteRule ^user_wall\.php$ /%2/%1? [R=301,L,NE]

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

The \w character class by itself is equivalent to: [A-Za-z0-9_]

Upvotes: 0

Related Questions