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.
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
I too use the second method (return), which seems to be regarded as being faster
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.
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
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.
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.
I use this:
http://pastebin.com/mg1mgknG
This way you can also use HTTPS.
check the official rewrite rules
http://nginx.org/en/docs/http/converting_rewrite_rules.html