301 Redirects to get Google-friendly URLs

I have read over and over again on Search Engine Optimization (SEO) blogs and websites, that:

  • Google hates duplicate content
  • Google considers a single website that can be reached via multiple URLs as duplicate content
  • Google considers www.website.com and website.com without www as different URLs

The simple fix is to use an HTTP 301 Redirect to point all of your URLs at one, so-called canonical URL. Your canonical URL is the one you consider your main domain.

Choose your canonical URL

It doesn’t matter whether you use www or the non-www version. You just need to be consistent throughout your site. All your links should use the same version. If your site includes a blog, make sure your site and blog URLs use your canonical URL too. In WordPress, you can set this in Settings>General.

Here are a couple of rationales for choosing:

  • Since www is no longer necessary, it makes sense to make the non-www version the canonical URL. It’s less typing and it’s what people are probably typing into the address bar anyway.
  • The main reason to use www is historical. I have a big website at www.spindrift.com that has been around for 18 years and it uses www consistently throughout. So for that website, the canonical URL is the www version.

How to redirect on Nginx

For the Apache server, there is a lot of information on how to do 301 Redirects in an .htaccess file. So for Apache, I’ll send you to Google to find out how to do it. However, ProsperOnTheWeb is on a virtual private server using the Nginx web server.

My original Nginx configuration allowed both the www and non-www version to reach my pages, with no rewriting.

server {
listen   80;
server_name  www.website.com website.com;

To do the redirect, I added an extra server block before the main server block. It specifies the non-canonical version and does a 301 redirect to my main, canonical URL.

server {
listen       80;
server_name  www.website.com;
return       301 http://website.com$request_uri;
server {
listen   80;
server_name  website.com;

Restart the server. Then test some of your web pages with both versions or the URL. You’ll see the browser convert the URL to the canonical one.

Credits: I found several resources describing how to do Nginx redirects. Thanks to Jeff Sebring Nginx 301 Redirects and the Nginx documentation.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.