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.


Published 1 September, 2008 by Patrick Oak

82 more posts from this author

You might be interested in

Comments (5)



htaccess only runs over Apche server. If you want to avoid google canonical problem, then it is the best way to use htacces.

almost 10 years ago



Thanks for this great post. I've been wanting to learn how to enable hot linking for a while now.


almost 10 years ago


Vivek Merani

URL Renaming is definitely very helpful. Our recommendation on the some other On-page Top 5 aspects would be be -

1. Sound Keyword Analysis

2. Analyzing websites which compete on these keywords.

3. Tagging all pages of the website in context with the page content.

4. Getting an Optimal Keyword Density i.e. using the relevant keywords the right number of times in the content of the website

5. Creating a XML Sitemap for submission on Google.

Hope this helps!

over 9 years ago


pondicherry times

how to redirect all pages to home which broken

over 8 years ago


Ganeshan Nadarajan

The article on techniques that can be implemented with htaccess is informative, Thanx for sharing.

over 6 years ago

Save or Cancel

Enjoying this article?

Get more just like this, delivered to your inbox.

Keep up to date with the latest analysis, inspiration and learning from the Econsultancy blog with our free Digital Pulse newsletter. You will receive a hand-picked digest of the latest and greatest articles, as well as snippets of new market data, best practice guides and trends research.