How to WWW Redirect the Right Way! (Apache, nginx and lighttpd)

Canonical URL is part of a problem for most blogs. Most people who concern about different versions of a URL that display the same content are likely worry about duplicate content issue. As we know, that is not good from the search engine standpoint. The solution is to www redirect — redirecting your blog from non-www to www or vice versa.

URL canonicalization goes beyond www vs non-www, but interestingly this topic alone has been discussed very often. The problem is, a lot of people get it incorrectly.

Let me elaborate…

The other day, I was browsing the WordPress plugin directories and saw many similar plugins that for www redirection using 301 permanent redirect.

What’s Wrong with “Typical” Solutions?

Don’t get me wrong. The problem is not is its simplicity. In fact, I’ve used a simpler plugin before that removes two meta header tags from standard WordPress header. If something allows me to accomplish something without modifying WordPress code everything I need to upgrade, I’ll take it if it serves a purpose.

This is quite different for 301 www redirection plugins. Most of them are not necessary anyway. One version extends functionalities to include Apache-based redirections, 404 monitoring and more.

I won’t name names, but the rest is just redundant. No, scratch that, they hurt the performance of your WordPress, although that might be just a bit.

For instance, a typical WordPress execution takes the following path:

Web browser → Web server → WordPress (PHP) → MySQL

Assuming that the data is placed within MySQL database, you need to go through the entire process, which adds delay every step of the way. The returning path is again as far, although the flow is exactly the opposite.

If your web server setup is optimized in a way that you don’t have to invoke PHP when processing static and other files (you should), you also have added CPU and memory usage to your server unnecessarily.

In other words, the cost is not only delay time, but also performance-wise.

Do you need to execute the WordPress core just to redirect www to non-www and vice versa? The answer is NO.

Let the Web Server Do the Work

All web servers I know of support this feature. You can either 301 (permanent) or 302 (temporary) redirect right in the web browsers, either to another page within the same domain or externally to the corresponding domain.

The flow of a redirection becomes:

Web browser → Web server

If you prefer the www version and the visitor uses non-www, the server will immediately return 301 Moved Permanently code in the header, including the new URI in the Location: header.

By the way, it has to be 301 redirection if you want to solve the canonical URL problem and avoid duplicate content.

Before implementing the redirection code, make sure you have think about the version you’d like to keep. I personally prefer the non-www version simply because it is shorter.

For the curious mind, the following gibberish code uses regular expression (or regex) to match the domain. For instance, the tilde sign (^) means start of a string. You don’t need to understand it though, unless you want to dig into advanced configuration.

Note: The following solution works for every blog and web site, and it doesn’t require any type of blog platform or software to work.

WWW Redirect with Apache

Note that you should put this into .htaccess file in your root directory of your web site, even if your blog is in a sub-directory.

mod_rewrite module is required for this to work. If you have used clean URLs or fancy URLs feature for your blog (permalinks), you already have this feature.

Here is the code to redirect www to non-www:

1

2

3

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]

RewriteRule ^(.*) http://%1/$1 [R=301,L]

An alternative which includes domain name is as follow:

1

2

3

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Put it close to the top or on the first line of .htaccess. You don’t have to modify a thing, edit your domain, or such. It works out of the box.

The following is the version to redirect non-www to www:

1

2

3

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

WWW Redirect with nginx

WWW redirect requires rewrite module, but it is included as standard HTTP modules during compilation of nginx. All Linux distribution packages that I’ve tested with include this feature.

Add this code to the top of the page, separately from the server {} section for the preferred canonical name.

For instance, if you like to redirect to non-www, add the following code:

1

2

3

4

5

server {

listen 80;

server_name www.example.com;

rewrite ^/(.*) http://example.com/$1 permanent;

}

The word permanent is key. It turns the redirect into 301 redirection. After this block, you may configure the domain without www.

Here is the code for redirecting non-www to www:

1

2

3

4

5

server {

listen 80;

server_name example.com;

rewrite ^/(.*) http://www.example.com/$1 permanent;

}

WWW Redirect with lighttpd

This trick requires mod_redirect module. You should put it close to the top of the configuration file before other redirect or rewrite rules.

Redirecting www to non-www can be done with the following code:

1

2

3

$HTTP[« host »] =~ « ^www\.(.*)$ » {

url.redirect = ( « ^/(.*) » => « http://%1/$1 » )

}

This version redirects non-www to www inside the same domain. Substitute example.com with your domain.

1

2

3

$HTTP[« host »] =~ « ^example\.com$ » {

url.redirect = ( « ^/(.*) » => « http://www.example.com/$1 » )

}

How to Edit Server Configuration

Usually web hosting providers allow you to edit configuration options per directory. Just drop a .htaccess on the root directory of your web documents. You can edit this directly by loging into your web hosting account or use the cPanel editor (or any web administration interface).

You don’t need to restart or reload your Apache server because it will read .htaccess in a directory when accessing it.

For lighttpd and nginx, you should have direct access to your configuration file. If not, ask your admin to edit it for you. Don’t forget to reload or restart to apply the new configuration.

viaHow to WWW Redirect the Right Way! (Apache, nginx and lighttpd).

Retour en haut