If your website is hosted on an Apache web server, .htaccess is a great way to create SEO-friendly URLs, take care of issues that can harm SEO and make your life as a webmaster more convenient.

The Basics

.htaccess is the name of Apache’s directory-level configuration file. When an .htaccess file is placed in a directory, Apache uses the configuration settings contained in it for that directory and any sub-directories.

The syntax used with .htaccess is quite basic yet .htaccess can be a powerful tool and is capable of controlling advanced, condition-based configurations.

For the official introduction to .htaccess, be sure to read the tutorial at the Apache website. What follows are some common and basic .htaccess techniques that are very useful.

301 Redirects

To make sure that all users go to www.yoursite.com even if they type in yoursite.com, use a 301 redirect.

Here’s the .htaccess code:

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

301 redirects can be used in other ways as well and are very important to SEO when you move pages.

For instance, if you have an old folder named files whose contents are being moved or removed, you can avoid an SEO blunder by redirecting anyone who tries to access files in that folder to another file (or folder) as such:

RewriteEngine on
RewriteRule ^files(.*)$ /some-other-page.html [L,R=301]

404 Redirect

To redirect users who receive a 404 error to another page, you can set this in .htaccess:

ErrorDocument 404 /notfound.html

This is a great way to reduce the risk that someone coming to your website via an invalid URL will be “lost.

Create SEO-Friendly URLs for Dynamic Pages

If you have a dynamic page called /news_articles.php and it is passed a numeric variable called id in the form of /news_articles.php?id=5, you can use .htaccess to create a more SEO-friendly URL structure such as /news-articles/5.

To accomplish this, use:

RewriteEngine on
RewriteRule ^/?news-articles/([0-9]+)$ /news_articles\.php?id=$1

Strip the Extension from Your Filenames

You’ll notice that the SEO-friendly URL in the example above lacks a file extension.

For aesthetics and security, it’s often desirable to be able to call static pages without the file extension. For example, the file http://www.yoursite.com/about-us/management-team.php could be called via http://www.yoursite.com/about-us/management-team instead.

To implement this, use this code:

RewriteRule ^/?about-us/([a-z–]+)$ /about-us/$1\.php

Protecting Images (and Other Files) From Leaching

If you host images or other multimedia files that others may be inclined to hotlink to at your expense, you can implement hotlink protection using .htaccess:

RewriteEngine on
OnRewriteCond %{HTTP_REFERER} !^http://www.yoursite.com [NC]
RewriteCond %{HTTP_REFERER} !^http://yoursite.com [NC]
RewriteRule [^/]+.(gif|jpg)$ – [F]

The code above will cause an image to break if the person trying to load it has been referred by a website other than yours.


These are but a few techniques that can be implemented with .htaccess. For a more thorough overview of .htaccess and how to use it, check out this guide.

There are also a couple of nifty tools here and here that can generate certain .htaccess configurations for you.