user3304007
user3304007

Reputation: 470

Using "?" in htaccess redirect

I want to redirect

mysite.com/vids.php?id=265s2g

to

mysite.com/watch?v=265s2g

I have tried

RewriteRule ^watch?v=(.*)$ vids.php?id=$1

But gives me a not found error. What is the correct way to do it ?

Upvotes: 1

Views: 184

Answers (2)

Robin C Samuel
Robin C Samuel

Reputation: 1225

You can use the following rule to redirect

RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC]
RewriteRule ^vids.php(.*)$ watch?v=%1 [L,R,NC]

The above rule will redirect,

http://anysite.com/vids.php?id=265s2g

to

http://anysite.com/watch?v=265s2g

Try the test at http://htaccess.madewithlove.be/

Upvotes: 0

anubhava
anubhava

Reputation: 785186

You can't match query string in rewrite rule. Use it this way:

RewriteCond %{QUERY_STRING} ^v=([^&]+) [NC]
RewriteRule ^watch$ vids\.php?id=$1 [L,R,NC]

Upvotes: 1

Related Questions