.htaccess File – 301 Redirect, .htaccess Rewrite, and Error 404.
.htaccess File – 301 Redirect, .htaccess Rewrite, and Error 404.
Hello everyone! As you probably guessed by the title of this post, today we will be taking a closer look at the .htaccess file. Many readers have been sending emails asking about .htaccess file and what it does. Instead of sending people to read and learn on they own, why not discuss it here, together? Discussing.
To remove the initial intimidation of the unknown, .htaccess file is nothing more than a regular text file. This file needs to be created in the notepad and saved in the root directory of your site.
In many cases .htaccess file will be already present in the root directory, which means you can just open and edit it in the notepad or via the built in FTP client.
What to write inside the .htaccess file?
To make it simple to understand, .htaccess file is a set of rules for the Apache core and the modules. There are many modules, but we are focusing only on mod_rewrite. (Back in a day, we had to make sure our hosting provider installed it. However, today, virtually all providers have mod_rewrite available for our use.).
Despite relative simplicity of the .htaccess file (all of the really scary things about Apache are located inside another settings file called httpd.conf, we will visit it in another post), a full list of all .htaccess settings (http://httpd.apache.org/docs/2.2/howto/htaccess.html) and, mod_rewrite settings (http://httpd.apache.org/docs/current/mod/mod_rewrite.html) are also not very small documents. Via the .htaccess file awebmaster can obtain an absolute control over the server, which fortunately is not required for SEO. In reality, all we (SEO professionals) need to know is some main commands. Let’s begin.
Comments in .htaccess file begin with “#” – a line that follows this symbol will not be processed by the web-server. I suggest commenting even the smallest and the simplest code, and after a year or two you will easily remember what that line was needed for. Unless of course, you are an elephant and can remember everything forever.
1. Avoid site’s page duplicates
Usually, the code for the main page of your site is located inside the /index.html (or index.php – for many dynamic sites). Let’s count all the possible ways we can open the site in our browser:
http://www.mysite.com
http://www.mysite.com/index.html
http://mysite.com
http://mysite.com/index.html
This is four different ways your site can be accessed by people and most importantly the search engines. If we don’t tweak our .htaccess file, our site will be added into the search engines’ index six exact same pages. This is a sign of a lower quality site. The .htaccess 301 redirect code below helps to avoid this misunderstanding completely:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.mysite.com/ [R=301,L]
All of the pages-duplicates will be glued via 301 redirect (permanent redirect) with the main page: http://mysite.com. All the weight of the domain name and your site’s main page will be focused in one place (instead of four).
2. Accurate error 404 page redirect
If we haven’t thought about the 404 error page before, right now is a good time to start. First we probably want to create a 404 error page and then go ahead and make it appear when a customer clicks on a link and page is not found. This can be done via .htaccess redirect to the error 404 page:
ErrorDocument 404 http://mysite.com/404.php
This same method works for any other types of errors other than error 404 as well.
3. Individual page and group redirects
In order to redirect a visitor from one page to another, we need to enter the 301 redirect code below into our .htaccess file:
Redirect 301 /old.html http://mysite.com/new.html
In order to redirect a group of pages, we need to use RedirectMatch instead of Redirect.
4. Saving files instead of opening them
Many of us have seen a bunch of unreadable text in the browser when trying to download an archive with .rar extension. This means that the .htaccess file on the hosting web-server doesn’t have a forced file type save command:
AddType application/octet-stream .rar .doc .mov .avi .pdf .xls .mp4
We can add more extensions if we need, there is no limit at all.
5. Creating permalinks (human-friendly)
I suggest using the settings provided by your CMS (WordPress, Joomla, Drupal, etc.). In fact, those long and unreadable urls appear only when using a CMS (static web sites don’t have them). Therefore, using the settings in the CMS will help avoiding this issue.
However, this is also possible to accomplish via the .htaccess rewrite by using the RewriteEngine On (turning on mod_rewrite). Making urls human-friendly using the .htaccess file is very time consuming. For example, in order to turn this url: http://mysite.com/script.php?cd=1776 to this: http://mysite.com/sale/cd/1776, we need to write these lines (RewriteEngine On has to be only written inside the .htaccess file if it wasn’t written in any line above it):
RewriteEngine on
RewriteRule sale/(.*)/(.*)/$ /script.php?$1=$2
It’s obvious that in order to successfully change all urls to human-friendly, we will need to learn all .htaccess rewrite commands and sytax. It’s much easier to use the settings inside our CMS.
6. Duplicate pages without a forward slash at the end of URL
In order to avoid the indexing of the pages http://mysite.com/about and http://mysite.com/about/ as two different pages by the search engines, we need to place this command code inside the .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]
This will make sure to redirect from the pages without the forward slash to the ones with the forward slash.
All of the examples in this post are not to be blindly copied inside our .htaccess file, especially the 301 redirect code. First we need to confirm that in fact, we have an issue that can be resolved with one of these .htaccess commands. Most importantly, we need to make sure to save a copy of our .htaccess file before we make any changes to it.
This is it for today. Once again I would like to say thank you for sending your emails with questions and comments. I read them all, no exceptions. This post came about because many people have emailed asking to visit .htaccess file from the SEO prespective.
“When there is a will, there is a way”. – Anonymous.
Beck @ SEO Blog
Similar Posts:
- What is 301 Permanent Redirect and How to properly use it?
- How to Speed Up WordPress – Part I
- Gain Page Rank – Black Hat SEO Method
- Daily TO DO List to monitor the health of your site
- What is NoIndex, Where to use it, and Why?





I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful
xyxytodwhy.2011