Redirect to another website using wordpress and querystring

I am new to WordPress and want to redirect some specific traffic. Here’s an example –

  1. A user from a different website clicks on a link with url http://mywordpresssite.com/redirect?url=http://amazon.in/blablabla

    Read More
  2. I want my website to redirect the request to
    http://amazon.in/blablabla or whatever be the url in the querystring without loading anypage of my website.

  3. My website should function normally for all other requests.

I tried with htaccess and came up with this –

RewriteCond %{QUERY_STRING} (?:^|&)url=([^&]+)(?:&|$) [NC]
RewriteRule ^(.*)$ %1 [R=302,L]

But the RewriteRule results in http://mywordpresssite.com/http://amazon.in/blablabla?url=http://amazon.in/blablabla which is wrong.

I am new to htaccess and would be grateful for any help.

Related posts

Leave a Reply

1 comment

  1. Try the following in .htaccess in the root of your site. Note that this should go before your existing WordPress mod_rewrite rules but after RewriteEngine On.

    RewriteCond %{QUERY_STRING} ^url=(.*)$
    RewriteRule ^redirect$ %1 [R=302,QSD,L]
    

    The %1 refers to the first parenthesised subpattern in the RewriteCond directive (ie. everything after the “url=”).

    The QSD flag (Apache 2.4+) removes the query string from the original request.

    Before Apache 2.4 you would need to change the RewriteRule to read (note the additional ? at the end of the substitution):

    RewriteRule ^redirect$ %1? [R=302,L]