Home | Search Engine Optimization | Technical SEO | Beginner’s Guide to the .HTACCESS File

Beginner’s Guide to the .HTACCESS File

John Doherty —  October 28, 2011
  • Buffer
  • Vote on Hacker News
  • SumoMe

Linkbuilding gets all of the love in the SEO community, but we often forget that technical SEO is the solid foundation upon which linkbuilding and SEO are built. If you don’t have your information architecture, redirects, and solid code in place, a lot of your efforts will be wasted.

Today I want to talk about the .htaccess file. There are many ways to implement 301 redirects (remember that 302 temporary redirects are bad news), but if you are using an Apache server, the best way to implement a redirect server-side is via the .htaccess file.

Note: if you’re not hosting on your own server, you must check with your hosting provider. Some hosting providers, such as Bluehost, will help you out with redirects, or at least redirecting a domain you bought for ORM reasons.

Finally, as an SEO you should know how to use one so that you can help out your clients when they come to you with problems like “Hey, so uh….our test subdomain is indexed.” After you respond with “Ruh roh!”, you have to help them.

One note: there are a lot of other issues that can happen with a .htaccess file. I don’t know to solve them all, but if you want to blog about other common occurrences and let me, I will gladly link to your post down at the bottom of this post.

Formatting a .htaccess file

When I first learned how to create and implement a .htaccess file, I was asking an industry friend:

“me: so all i need is this?
redirect 301 / http://www.newdomain.com
none of that RewriteEngine on stuff?

His nice response: “that engine rewrite stuff is the format you used for htaccess”
Me: “….”

The .htaccess syntax is a bit more complicated

Simple redirecting of a placeholder domain to another

If you’ve bought a domain for online reputation management purposes (ie the .net of your .com), here’s the correct format for a .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com$[OR]
RewriteCond %{HTTP_HOST} ^examplecom$
RewriteRule ^(.*)$ http://www.mainsite.com/$1 [R=301,L]

Let’s explore these individually.

(Power Button photo) (img source: http://visualchiropractic.com/sitebuildercontent/sitebuilderpictures/PowerButton2.jpg)

RewriteEngine on

This command tells the server what you are asking it to do. With this line, you are telling your server that is is supposed to follow the redirect commands coming up.

RewriteCond %{HTTP_HOST} ^www.example.com$[OR]
RewriteCond %{HTTP_HOST} ^example.com$

Here, you are giving conditions for which parts of the domain to redirect. In the above example, I am telling the server to redirect “http://www.example.com” AND “http://example.com” to a different place (see next example).

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

This little beauty of a line of code tells your server where to redirect the above conditions. The “R=301” tells that server to return a 301 “permanently moved” header to crawlers, and “L” means that this is the last rule to run.

Where do I put the file?

The .htaccess file lives in the root folder of your domain. This is where your robots.txt file is located, as well as other files such as your 404 page. The easiest way to upload your .htaccess file is via FTP, though if you have access to a CPANEL you can often to it from there.

Common .htaccess issues and how to solve them

“My test subdomain is indexed”

Whoops! This one is fairly simple to solve IF you do not have other subdomains on that same server (for example, mail.domain.com). If mail.domain.com is on a different server, then you’re good to go.

Here’s what you should use:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

“I’m migrating my site and have to do server-side 301 redirects”

If you’re migrating a whole site and can’t just do conditions in your .htaccess, you can also do one-to-one redirects thus:

Redirect 301 (old URL) (new URL)

If we’re redirecting, for instance, the http://www.example.com/ugly-url page to http://www.newexample.com/pretty-url, do this:

Redirect /ugly-url http://www.newexample.com/pretty-url

If we’re redirecting an old file path to a new file path, such as http://www.example.com/files/oldfile.pdf to http://www.newexample.com/documents/newfile.pdf do this:

Redirect /files/oldfile.pdf http://www.newexample.com/documents/newfile.pdf

Syntax is:

Redirect /olddirectory/oldfile.pdf http://www.newexample.com/newdirectory/newfile.pdf

Redirect all subdomains to main URL

If you’ve done some online reputation management and bought some domains that are similar to your own (for example, Google owns www.foofle.com, which redirects you to www.google.com. Little known piece of trivia for you!), then here is the format you will want to use:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$[OR]
RewriteCond %{HTTP_HOST} ^example.com$

Uh oh! I’m getting a redirect loop. What did I do?

If you update your .htaccess file and all of a sudden you have redirect loops, make sure there’s a $ stop at the end of your condition. This is the most common reason why a redirect loop will occur.

Second, check to make sure you are not 301 redirecting your page back on itself. Be careful especially with redirecting your domain.com/index.html to your domain.com. This code should do the trick for the /index.html back to your homepage:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]

A couple of pro tips for you

WordPress has some awesome plugins for redirection, which makes life simple if you only need to redirect a few URLs.

Many hosting services will also allow you to redirect entire domains. I personally use Bluehost for my own sites and they make this easy.

By the way, if you are interested in learning RegEx (regular expressions) better, which is useful for technical bits like .htaccess but also advanced segmentation in Analytics, check out this Regular Expression Guide for SEOs on SEOHimanshu.

Also, Mike Essex from Koozai put up a helpful video here.

And if you’re feeling really developer-y, check out the official Apache full features for .htaccess

John Doherty


Founder of Credo, I'm formerly the Senior Growth Marketing Manager at Trulia Rentals. Previous to Trulia Rentals I ran marketing at HotPads.com. Previous to that I worked at Distilled for 2+ years as an online marketing consultant. In my spare time I shoot lifestyle photography, ski, rock climb, and update my Twitter account.

2 responses to Beginner’s Guide to the .HTACCESS File

  1. John, since this is a “Beginners Guide” to .htaccess lets review a couple more things. When dealing with a RewriteCond or RewriteRule there are some things you are using here that I wanted to explain.

    Q: What are the “^$” used for? A: The ^ is used to mark the start and $ marks the end. Why use these? These characters are not valid URL characters and can be used in the rule engine of the Apache webserver.

    Q: Why do you use “\” in the file names? A: The “\” character means “literally”, and a “.” really means match any character but when done together they mean “Really I want you to literally match the period”. So to match “index.html” you would say “index\.html”.

    Q: Why do you use “()” in the RewriteRules? A: The “()” tells the rules engine to “remember” what was matched. To to match everything “.*” and remember it “(.*)”. Now for each match that is remembered it is stored in a token from first $1 through all $???? (big number of matches).

    Q: What is this “[A-Z]” that I see above? A: Well the “[]” means set, and “-” is range. So “[A-Z]” is just a simple way of writing “[ABCDEFGHIJKLMNOPQRSTUVWXYZ]”.

    If I can think of other helpful “intro” items for beginners I will post them.


    • Heath –

      Thank you for these helpful additions! Regex like this is quite challenging to learn and I am honestly still trying to wrap my head around it. Your comments and explanations are very helpful!

      Also, sorry my font is screwing up your carats and turning them into “^”s.