Today we are going to talk about some of the powerful things you can do with your .htaccess file. .htaccess is primarily used to give instructions to Apache servers before a page loads, it can however also be used in Windows environments where Helicon ISAPI Rewrite 3 module is also available.

Note: .htaccess is very powerful, but it is also very temperamental. Get somthing wrong and you can easily take down large parts of your website, if not your whole site. Be careful when typing out lines of code

So with that warning out of the way lets dive in and see what we can do with .htaccess.

Fix Canonicalisation Issues

If a user visits your website at and they are not redirected to this causes potential duplicate content issues. To fix this we need to put in place a 301 redirect that catches all pages of the site that have no www. and redirect it to the correct page.

The Code

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

Redirecting a Page to a New Location

It happens to the best of us. We create a page in one location only to modify our site architecture later on. To maintain any link equity that the page may have gained, maintain users bookmarks and let Google et al know where the new page is we need to add a 301 redirect into the .htaccess file.

The Code

RewriteEngine on
RewriteRule ^old-category/page-name/$ /new-category/page-name/ [L,R=301]

We can also change folder names in a similar way.

The Code

RewriteEngine on
RewriteRule ^old-category/(.*)/$ /new-category/$1/ [L,R=301]

404 Catch All

Every now and again a user might encounter a 404 error on your website, be that through a link that has been copied incorrectly or a page that has been deleted. It is best practice to serve a 404 page to these users that shows them a way to find either the content they where looking for or newer more relevant content.

The Code

ErrorDocument 404 /404.html

Prevent Image Hotlinking

People using your images on their website is annoying at the very least, illegal at worst but there is another downside to people who embed images directly from your website – you are paying for it, through your bandwidth costs. Preventing this requires just a few simple lines in your .htaccess file.

The Code

RewriteEngine on
OnRewriteCond %{HTTP_REFERER} !^ [NC]
RewriteCond %{HTTP_REFERER} !^ [NC]
RewriteRule [^/]+.(gif|jpg)$ - [F]

SEO Friendly URLs

Many CMS and eCommerce systems use parametes to create their dynamic pages. These pages will often call an id or a filter in the format:

A much more SEO friendly URL would be:

To achieve this we would use the following code.

The Code

RewriteEngine on
RewriteRule ^/(.+)/(.+)/([0-9]*)/([0-9]*)/$ /products.php?product_id=$3&type=$4

Remove File Extensions From URLs

Many static websites will have .html at the end whilst many dynamic sites may have .php or .aspx. Using these file extensions immediately gives and hacker an indication of which technologies you may be using for your website, allowing them to tune their toolbox into making hacking your website easier. The other thing is it simply doesn’t look pretty either.

So how would we go about removing the file extensions? (The below example makes the assumption that your file extension is .php)

The Code

RewriteEngine on
RewriteRule ^(.+)$ /$1.php


As you can see there are many powerful uses for .htaccess beyond 301 redirects that can be used for usability, SEO andeven save you cash!

Do you have any others? Share them below and we will add them to the post with a credit to yourself.

About the author Daniel Taylor

Dan Taylor is the founder and owner of Tailored Internet marketing. With more than 5 years in SEO Dan has worked with and for brands such as MoneySupermarket, JJB Sports, and others. Dan is a father of two, and avid Everton FC fan.

Comments are closed.