Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to redirect all users coming to a URL "" to a URL like "".

I'm trying with code like this:

server {
  server_name "";
  rewrite ^(.*)$1 permanent;

By accessing "" I get redirected to "", but by accessing "" I get redirected to: "".

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

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

share|improve this question
up vote 0 down vote accepted

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

If you just want to redirect /something, and no other URL, then:

rewrite ^(/something.*)$1 permanent;

That'll send a request for to,

and, say, to

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 to – 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 ^(/.*)$1 permanent; Nothing else worked. The access was then done trough* – user93656 Feb 21 '13 at 14:19

You can do either:

 rewrite ^/(.*)$$1 permanent;

That brings you:

$ curl -I

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


rewrite ^(/.*)$$1 permanent;

You can see that it also works:

$ curl -I

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

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

Your Answer


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.