Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


[QUESTION] NGINX 301 Redirect, Rewrite vs Return
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

[QUESTION] NGINX 301 Redirect, Rewrite vs Return

Hello there! currently I am using rewrites to redirect www.domain.com to domain.com. someone suggested me that instead of:

server {
server_name domain.com;
    rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
}

I should use:

return 301 $scheme://www.domain.com$request_uri;

Since according to him a return is quicker than a rewrite, and $scheme is protocol independent.

What do you think?

Comments

  • I'm also not sure which one is better, but right now i'm using the second method to redirect www to naked domain.

  • Semantics, for the most part.

  • Thanks @budi1413 , I know that both work but I am note sure which one is better

  • @budi1413 said:
    I'm also not sure which one is better, but right now i'm using the second method to redirect www to naked domain.

    I too use the second method (return), which seems to be regarded as being faster

    Thanked by 1Santiago
  • danodano Member
    edited November 2013

    Either way, it seems your returning a 301 with either method, and that should be valid for SEO,etc?

    I use this to redirect non-WWW requests to www requests, so that the site doesn't get dinged for duplicate content.

      if ($host = 'crummysite.org' ) {
            rewrite  ^/(.*)$  http://www.crummysite.org/$1  permanent;
         }
    
      curl -I crummysite.org
      HTTP/1.1 301 Moved Permanently
      Server: nginx/1.5.6
      Date: Wed, 27 Nov 2013 15:26:03 GMT
      Content-Type: text/html
      Content-Length: 184
      Connection: keep-alive
      Location: http://www.crummysite.org/
    

    Of course, the above can be used either way, to redirect www to non-www, or vice-versa.

    Which way is better -- I am not sure, but I think if they are both correctly informing the browser or www client of the 301, I guess it comes down to which syntax you think is prettier in your conf files :)

    ps-- had to edit to fix my crap(fav word of the day) formatting

    Thanked by 1Santiago
  • regexes are expensive, and avoiding them with the second method will be faster, although you probably won't feel the difference at the end of the day.

    Thanked by 1Santiago
  • Thank you guys

  • second one, although I use php for this :P

  • The second is better as the rewrite is done, the first approach will start a new rewrite search.

  • n0myn0my Member
    edited November 2013

    I use this:

    http://pastebin.com/mg1mgknG

    This way you can also use HTTPS.

  • Thanked by 1marrco
Sign In or Register to comment.