4 Nginx SEO Tricks For Your nginx.conf Server

Easily found SEO hacks for Apache is half the reason why some folks never switch to Nginx. These tips will help you convert without SEO loss and the gain of a lighter, more efficient web server.


Generic 301 Redirects For Old Content

Its always better to handle large wildcard directory redirects in your servers configuration rather than inside of a CMS if you can help it. You avoid your CMS processing anything if you can handle it at the server level. By processing I mean any extra database query that has to be done/searched before it can figure out where it needs to go.

# Rewrite anything under a certain path to a certain directory.
if ($request_uri ~* "^/anything/in/this/path(/.*)?$") {
    rewrite ^.*$ /send/to/here/ permanent;
# Rewrite anything under a path to a different path maintaining the file being called.
rewrite ^/it/was/here(/.*)?$ /now/it/goes/here$1 permanent;
# Single file rewrite.
rewrite ^/this/file.php$ /is/now/here.php permanent;

Redirect Index Requests

This will help to avoid duplicate pages and redirect any nosy bots. You might need to change “index” to “default” if you’ve configured your setup differently. If that’s the case you might still want to use the index version of this as well.

rewrite ^(.*)/index.(.*)$ $1 permanent;
rewrite ^(.*)/default.(.*)$ $1 permanent;

WWW vs Non-WWW Requests

Either method is OK. There really is no right or wrong to using WWW or Non-WWW. Just a preference thing. It matters more that you pick one and stick to it. I use it without because it looks neater/shorter but this will cover both scenarios.

If your website has any amount of hard coded links with more of one versus the other then that will probably be a major deciding factor for you on which you choose. Unless you want to go edit the larger group of links.

# Replace with your domain
if ($host = {
rewrite ^/(.*)$$1 permanent;
#Or, redirect to non-www
if ($host = {
rewrite ^/(.*)$$1 permanent;

Redirect Multiple Trailing Slashes

Sometimes users accidentally do this:

Sometimes obnoxious bots purposely do this:

This will solve both of those scenarios. Be careful with these though. If you’re running a CMS like WordPress or Drupal then those systems might already be handling this depending on what you’ve installed and configured. If you do it at this level, don’t do it at the CMS level. You’ll run into redirect loop issues.

# Leave one trailing slash
if ($request_uri ~* "^/{2,}$") {
rewrite ^.*$ http://$host? permanent;
# Remove all trailing slashes
rewrite ^(.*[^/])$ $1/ permanent;

If you’re trying to remove slashes you might need to declare

server_name_in_redirect off

In your server variables. Read more on that at the official Nginx Site.

Like the WWW versus Non-WWW you’ll probably want to make your decision to use a trailing slash or not based on existing hard coded links. Just so you limit the amount of edits you’ll have to go back and do. Otherwise a lot of your in-body links will be 301ing throughout your site. Inefficient and bad SEO practice. Note, this only applies to internal links so don’t waste time correcting your external URL’s.

Researching Your Own Slashes vs No-Slashes & WWW vs Non-WWW Indexing

The reason you do this is to avoid duplicate content penalties. Google sees the alternative to each as a separate, but identical in content, page. Even worse, if you have that going on AND you can clearly see you’re indexed for each scenario then you’re probably linking to each scenario somewhere within your site.

While you’re making a decision between the two options and aren’t sure how you’ve hard coded your links you’ll want to comb through some “site:” results in Google. IE. do a search for “” in Google and compare the results to ““. Without using the WWW search here you can already see “” chose to use WWW and slashes. Sometimes you have to look at the omitted results to see any overlap. Segregate portions of your site on a more specific path if you have a lot of content.

If you noticed, is optimized (but also running an Apache server). How did I know that? Server Spy.

Canonical Reinforcement

You can reinforce your choices by using Automatic & Manual Canonical URL Links In Drupal too. This example applies to Drupal more specifically but you can still extract the concept of what you need to do there by looking at the manual insertion of canonical URL’s section.

Respond: Leave A Comment | Trackback URL

Entrupeners, Subscribe for the lastest tools, tips, and tutorials.

Leave a Reply

Custom Theme by Rob Malon | Content & Design © 2010 - RobMalon.Com - Chicago, Illinois