Nginx Redirect – How to Redirect Requests to a Different Domain/URL

nginxredirectrewrite

I'm trying to redirect all users coming to a URL "http://example.com/something" to a URL like "http://answares.com/examples_com/something".

I'm trying with code like this:

server {
  listen 1.2.3.4:80;
  server_name "example.com";
  rewrite ^(.*) http://answares.com/examples_com$1 permanent;
}

By accessing "http://example.com/" I get redirected to "http://answares.com/examples_com/", but by accessing "http://example.com/something" I get redirected to: "http://answares.com/something".

I tried to do it in different ways but the best I got was:

http://answares.com/examples_com//something

Which because of the two slashes looks lame.
I'm using Nginx 0.7.65 on Ubuntu 10.4

Best Answer

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.