Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 0 down vote accepted

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

share|improve this answer
    
But /something is not fixed for me. It could be /somethingelse too. –  user93656 Sep 3 '11 at 20:10
    
@user93656 Do you want to redirect everything that isn't under /examples_com, or do you want to specifically define each location to redirect? –  Shane Madden Sep 3 '11 at 20:20
    
I want to redirect anything from examples.com/ to answers.com/examples_com/ –  user93656 Sep 3 '11 at 20:29
    
The config you have in your question does that. Are you sure you restarted nginx after making those changes? –  Shane Madden Sep 6 '11 at 19:03
    
I had to do: rewrite ^(/.*) answers.com/a/b$1 permanent; Nothing else worked. The access was then done trough example.com/a* –  user93656 Feb 21 '13 at 14:19

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|improve this answer
1  
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. –  Shane Madden Sep 6 '11 at 19:11

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.