Magnet
Magnet

Reputation: 63

Redirect url to another url with query string

I want to redirect with .htaccess

http://www.site1.com/pages/?url=http://www.gooogle.com/xyz/jkjk.html

to

http://tracking.site2.com/s?key=123456789&url=http://www.gooogle.com/xyz/jkjk.html

RewriteEngine On
RewriteCond   %{REQUEST_URI}    ^/pages/?$
RewriteCond   %{QUERY_STRING}   ^url=(.+)$
RewriteRule   ^(.*)$ http://tracking.site2.com/s?key=123456789&url=$1  [R=301,L]

Tried this.. failed. Please help me with this

Upvotes: 1

Views: 69

Answers (2)

user2934155
user2934155

Reputation:

If you want the query string, add an additional flag of QSA (Query String Append) like so: [R=301,QSA,L]

Upvotes: 1

anubhava
anubhava

Reputation: 786091

You can use this rule in root .htaccess of server1:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^url=([^&]+) [NC]
RewriteRule ^pages/(.*)$ http://tracking.site2.com/s?key=123456789&url=%1 [QSA,NC,NE,R=301,L]

Fro back referencing captured group from RewriteCond we need to use %1, %2 etc instead of $1, $2.

Upvotes: 1

Related Questions