How to Use .htaccess File and mod_rewrite to Improve Your Website

The .htaccess file is a configuration file for Apache web servers that allows website administrators to customize server settings and add configuration rules on a directory-by-directory basis. Utilizing the .htaccess file adds a very small amount (milliseconds) of latency to your website because the web server has to process extra rules. Therefore it is recommended that the .htaccess file only be used when you don’t have access to the main server configuration file. That would likely be the case if you do not operate your own servers and your website is hosted on a shared hosting environment.

The most common placement of the .htaccess file is adding it to your website’s home directory so that the rules also apply to all the subdirectories. There are many things you can do with the .htaccess file, but I will discuss some basic necessities that improve search engine optimization and ultimately enhance usability for your visitors. I will show you how to use mod_rewrite rules to accomplish this goal. Apache web servers are constructed with many modules available and one of the most useful modules is mod_rewrite. The mod_rewrite module lets you redefine and manipulate URLs based on rules written using special directives (you can think of directives as syntax) and regular expressions. You define these rules within the .htaccess file.

Turn on mod_rewrite in the .htaccess file

Let’s dig right in! Create a blank file with extension .htaccess. Note that .htaccess is the extension and not the file name. You must make sure it is that specific extension with no file name, otherwise it will not work. These two lines specify that you will be following symbolic links and mod_rewrite is to be turned on. Symbolic links are similar to file shortcuts. It allows for URL manipulation with a added layer of security to your web server. Therefore it is required for mod_rewrite to work.

Options +FollowSymLinks
RewriteEngine on

Forcing www or non-www on a website

By default the same website can be reached with or without a www prefix unless you force one of these options. When you force an option, the user is automatically redirected to that option. Why would you want to force an option? Because search engines sometimes see website domains with the www prefix and without the www prefix as two separate entities. If you leave it as is, this affects your search rankings because now all of your content is considered to be duplicated and overall relevance becomes increasingly ambiguous. You can go with either route and have the www or not, but it is important to choose one option and stick with it. RewriteCond and RewriteRule is equivalent to an if/else statement in many programming languages where RewriteCond defines the condition and RewriteRule defines the action to follow if the defined condition matches. Choose the option that you want to go with and add the corresponding code to your .htaccess file:

To force www:

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

To force non-www:

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*)$1 [R=301,L]

Note: Replace with your domain

301 redirects

Every action on the web is correlated with a status code. 301 is the status code for a redirect. Think of a 301 redirect like a physical change of address in real life. When you move from one residence to another you have to alert the post office and people who send you mail of your recent move so future mail can be redirected to your new residence. The same concept is true for the internet and 301 redirects allow you to do this with great granularity. It is much more common for someone to restructure directories and change certain URLs within a website than move to a brand new domain. You can alert search engines and future visitors of your website that you’ve shuffled URLs and directories by adding 301 redirects in the .htaccess file. Search engines and users do not like to waste their time. 301 redirects prevent search engines from trying to crawl old pages and users from getting old broken links.

Redirect specific pages

Redirect 301 /oldpage.html

Redirect specific directories

RedirectMatch 301 ^/olddirectory/

Note: Replace placeholders with your actual page URLs

Custom error pages

There are a few types of errors that you can get from a web server. The most common one is a 404 error which is the code for a requested page that does not exist. The user could have accidentally typed in the wrong URL or it was an old page that no longer exists at all. Web server error pages by default are not very user-friendly. It conveys very minimal information only relating to the error. This is why it is better to define a custom error page that allows you to add more useful information, perhaps a better design interface and/or other links that you want to direct the user towards. You can serve custom error pages using the .htaccess file:

ErrorDocument 404 /404.html

Note: Replace /404.html with the path to your custom 404 error page

This configuration setting leads users to a more user-friendly file that you created whenever a 404 error happens.