How to redirect requests to a different domain/url with nginx
Solution 1:
If you just want to redirect /something
, and no other URL, then:
rewrite ^(/something.*) http://answares.com/examples_com$1 permanent;
That'll send a request for http://example.com/something/
to http://answares.com/examples_com/something/
,
and, say, http://example.com/something/somewhere/page.html
to http://answares.com/examples_com/something/somewhere/page.html
Solution 2:
You can do either:
rewrite ^/(.*)$ http://answares.com/examples_com/$1 permanent;
That brings you:
$ curl -I http://xxxxx.com/some/example/url/here.html
HTTP/1.1 301 Moved Permanently
Server: nginx/0.8.53
Date: Mon, 05 Sep 2011 13:48:23 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://answares.com/examples_com/some/example/url/here.html
or
rewrite ^(/.*)$ http://answares.com/examples_com$1 permanent;
You can see that it also works:
$ curl -I http://xxxxxx.com/some/example/url/here.html
HTTP/1.1 301 Moved Permanently
Server: nginx/0.8.53
Date: Mon, 05 Sep 2011 13:47:09 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://answares.com/examples_com/some/example/url/here.html
The difference between nginx redirect and mod_rewrite redirects is that nginx doesn't remove the slash(/) after the server name before matching.
In order to remove the double slashes you have, you sould first match the slash in the regexp withouth the parenthesis, and then apply the match; or match everything and apply the match without parenthesis. Using one way or another is just a matter of taste ;-)
Solution 3:
The fastest way is to do a return
instead of a rewrite. see here:
nginx redirect to www.domain
I just answered the linked question and tumbled upon this one.