How to redirect requests to a different domain/url with nginx

123,757

Solution 1

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.

Solution 2

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 3

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 ;-)

Share:
123,757

Related videos on Youtube

Nandatto
Author by

Nandatto

I'm a passionate computer enthusiast with a broad spectrum of knowledge. Since 2016 I've been mostly working with Scala, Erlang and Node.js. I worked with Enterprise Java, C and Linux for the most part of my career.

Updated on September 18, 2022

Comments

  • Nandatto
    Nandatto over 1 year

    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

  • Nandatto
    Nandatto over 12 years
    But /something is not fixed for me. It could be /somethingelse too.
  • ravi yarlagadda
    ravi yarlagadda over 12 years
    @user93656 Do you want to redirect everything that isn't under /examples_com, or do you want to specifically define each location to redirect?
  • Nandatto
    Nandatto over 12 years
    I want to redirect anything from examples.com/ to answers.com/examples_com/
  • ravi yarlagadda
    ravi yarlagadda over 12 years
    The config you have in your question does that. Are you sure you restarted nginx after making those changes?
  • ravi yarlagadda
    ravi yarlagadda over 12 years
    Good explanation, and exactly right on how the rule should be laid out - but, your examples are functionally identical to the rewrite in the question, except that they won't tolerate a lack of a trailing slash on a request for just the domain. .* matches the slash, and is greedy so it'll take the full string ($ is implied). To clarify on Apache, it only strips the slash based on current context when in a Directory block or .htaccess file, or if a RewriteBase is used to modify the path.
  • Nandatto
    Nandatto about 11 years
    I had to do: rewrite ^(/.*) answers.com/a/b$1 permanent; Nothing else worked. The access was then done trough example.com/a*
  • raksheetbhat
    raksheetbhat over 4 years
    Thanks for the curl -I flag. Viewing request header information can be very useful.