redirect www.mydomain.com to www.mydomain2.com/try.html

My nginx server has two server_name: mydomain.com and mydomain2.com

I’d like to redirect all requests from mydomain.com to mydomain2.com/try.html using a rewrite with the referer.

I am trying to do it with this rewrite:

               if ($http_referer ~* (www.mydomain.com) ) {
               rewrite http://mydomain2.com/try.html permanent;
       }

But it does not work.

Could someone help me…?

Regards and thanks.

Answer

Taken from nginx wiki: Pitfalls

Your rewrite rule isn’t matching for beginning of the string. Use a ”^” to do so.

rewrite ^ http://mydomain2.com/try.html permanent;

Alternatively, you can use

return 301 http://mydomain2.com/try.html;

Attribution
Source : Link , Question Author : Emmanuel , Answer Author : Michael Hampton

Leave a Comment