Simple .htaccess Tricks for On-Site Optimization

This post goes out to all the site owners/developers out there who are running a site using the Apache server. You want to optimize your site for search results, but the whole task seems rather daunting. We’ll be honest, it can be. But before doing keyword research, building landing pages, and perfecting your internal linking structure, there are some basic on-site modifications you can make first. This list represents some of the most common on-site mistakes SEOs encounter. Luckily, they are also some of the easiest to repair. All of these changes can be implemented by simply adding a few basic lines of code to the site’s .htaccess file.

How to Edit the .htaccess

Accessing the .htaccess:

Using an FTP client, you can log in to your domain and edit the .htaccess file from there. (My preferred FTP client is Filezilla.) Your FTP login should be available from whatever host you are using (e.g., GoDaddy, Bluehost). For those of you who have access to a cpanel (control panel), the .htaccess file will be available from your File Manager. If unable to find it immediately, make sure you display hidden files, as sometimes it is hidden by default. In most cases, you should already have an .htaccess file available to select from the existing files; otherwise you will need to create one.

Creating an .htaccess file:

If there is no sign of a .htaccess file, you can create one. (You’ll want to double or triple check this and cross-source with some other resources before reaching this conclusion, to make sure you are not creating a duplicate.) Open a text editor program (Notepad for PCs or TextEdit for Macs). Make sure to name the file exactly “.htaccess,” not “htaccess.txt” or any other file extension. Only use lowercase letters and make sure a dot precedes the “htaccess.”

Simple Tricks for SEO

Once you have created or opened the .htaccess file, you are ready to make some edits. Each of these simple lines of code make a BIG difference as far as overall site health and performance. If you are unsure whether or not your site includes these directives, it is worth checking. We generally recommend that every site have these included in the .htaccess file.

1. Disallow index files.

Every site is built in terms of directories, which are essentially folders. Each of these directories contains files that represent individual web pages or pieces of media. However, in many instances, a particular file within the directory is not called out in the URL. So for example: Thus, each directory should include an index page (e.g., index.html). These index pages essentially tell the server to display this page by default if a specific file is not requested. When a server is told to look up a directory that does not contain an index file, all of the files in that folder will be displayed instead (see image below). In other words, if the server is not directed on which file to display by default, it displays a list of all of them.

Website directory that does not contain an index file

Why should you care? For one, there are potential security issues that come along with this. It’s not enough to be a major security problem on its own, but if someone is already looking to breach your security, it’s effectively giving them a tall glass of lemonade. aHere you go Mr. Hacker, thanks for all of your hard work. Enjoy!a Another, possibly more important, issue is bad user experience. This page is ugly to say the least. It’s definitely not helpful to the user. If you’ve ever stumbled upon one of these pages yourself, I can imagine you didn’t stay for long trying to figure it out. How To: Disallowing index files prevents servers from producing directory listings for anyone to see. Insert the following code into the root folder of the .htaccess file:

Options -Indexes

Now, if someone enters a directory name that is missing an index file, they will be shown a 403 Forbidden Error. As discussed later in this post, this page can be easily customized in order to give the user instructions on what to do next.

2. Redirect www to non-www (or vice versa).

Google treats and as completely different pages from one another. Without redirecting one page to the other, most sites have identical pages located on both versions. Why Should You Care? If there are links to both the www and non-www versions of pages within a domain, this can lead to what is known in the SEO world as “duplicate content.” It’s bad because it splits up the authority of these pages among the different versions, which have acquired links. It can also, in rare instances, lead to penalties. Therefore, it is important to set a preferred version for every website and redirect all URLs within the domain to that version. Whether you prefer the dub or non-dub is totally up to you, search engines do not have a preference. How To: Using 301 Permanent Redirects, all of the authority of the non-preferred pages will be moved to the canonical (preferred) version. To redirect to the www version of the site, insert the following code into the .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

To redirect to the non-www version of the site, insert this code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

(Remember to insert your domain name where it says “example.”)

3. Remove Directory Indexes from the URLs

We already discussed how directory indexes (i.e. “index.html”) are important. We want these to exist, definitely. But we donat necessarily want them to appear at the end of the URL. Why Should You Care? While these pages are helpful to the server, the directory index does not help the user. In fact, it is just one more parameter for them to type into their browser, or to copy and paste, or what have you. Also, this can be another big contributor to duplicate content, as discussed earlier in this post. If the same page exists at “” and “”, this is no good for SEO. One way to clean up a site’s URL structure is to remove extra directory indexes from the URL. How To: Add a rewrite rule that instructs the server to redirect all pages with that ending parameter to a URL without it. Insert the following code to the .htaccess file:

RewriteCond %{THE_REQUEST} /index.html HTTP [NC]
RewriteRule (.*)index.html$ /$1 [R=301,L]
4. Create Custom Error Pages

Even the healthiest sites have errors every now and then. These most commonly include 404 Not Found errors, for pages that have been deleted or moved. This also includes things such as 500 Server Errors, 403 Forbidden Errors, etc. The causes for these vary, and there are different ways to fix them based on SEO best practices depending on the cause. But that’s a topic for another day. Create custom error pages The first, and easiest step, is to create custom error pages. This ensures that if a visitor does encounter one of these errors, they will be less likely to abandon ship. Why Should You Care? From a user experience perspective, errors can be detrimental. If you’ve ever seen an ugly error page like the one above, I’ll take a wild guess that you didn’t stay on that site very long. Errors are still a big problem and should be a priority to fix. However, the process can be a long one. In the mean time, you have the opportunity to salvage some of the potentially lost revenue and bad user experience by creating a custom page. Custom pages can include an apology for any inconvenience and direct instructions on what the user should do next. How To: In order to do this, you just need to reference custom pages in your .htaccess file. Add the following lines to your .htaccess, one for each custom error page you’ve created:

Error Document NUM /folder/name.extension

Some examples:

ErrorDocument 404 /errors/not-found.html
ErrorDocument 500 /errors/server.html

Back Up and Test!

Please remember to:

1. Back up your .htaccess file. Before you make any of these changes, save a copy of your .htaccess file! If something does go wrong for whatever reason, you will be able to re-upload the original .htaccess, preventing you from going in to panic mode. 2. Test your site. After making any change to the .htaccess file, make sure to test out the site to make sure it’s working. Checking after each update will help determine where/if a problem arises.


As SEOs, we hate to see an awesome site with simple errors holding it back. Many site owners donat realize that these issues exist and how easily they can be repaired. We encourage you, as a site owner, to ensure that these errors are avoided/corrected as much as possible. It doesn’t take a rocket scientist, or even a development background, to fix some core issues that could be preventing a site from performing in search.