Redirecting non-www to www-based sites
The debate about using a non-www prefixed URL versus including the www still rages on in the SEO world, but the key thing is to ensure there is consistency.
This means that if you choose to have the www
prefix, then you'll want to ensure all URL calls are to this pattern. Especially since there are SEO implications involved with any URL change, attention to detail is critical here. For this recipe, we'll redirect all non-www to www-based calls, but the reverse is simply a matter of slightly modifying the rewrite rule.
How to do it...
If you're used to the Apache way of doing things, this may look a little different. In the NGINX world, if is evil and needs to be avoided at all costs. Instead, we set one server
block up for the non-www site and set it to redirect and a separate server
block for the www-based site. Here's an example code:
server { listen 80; server_name redirect.nginxcookbook.com; return 301 http://www.redirect...