Ultimate Guide to the Use of .htaccess for SEO


Search Engin Optimization is all about increasing your website traffic through organic search engine result and help to rank the pages and posts for your website. In today’s world competition, you can not just neglect SEO. To get better performance of your website in SEO .htaccess allows the developer to override the server configurations to your requirement. By replacing the main server’s configuration files to your own, .htaccess file is loaded to the webserver. After detecting the customize configurations, executed by the server to alter the configuration of the software for custom functionality and features that the software typically doesn’t offer. So by setting up the configuration of certain server files, SEO performance is increased and you are able to customize the area by your own requirement the server may not offer.

What is .htaccess file?

.htaccess is a configuration file used by Apache Web Server Software. Through .htaccess file, you can change the configuration of Apache Web Server to enable or disable rewrite rules and some functionality the Apache Web Server has to offer. Note .htaccess files must be uploaded in ASCII mode (binary mode is not supported). Some basic functionality that can be controlled via Apache .htaccess is:

  1. Setting 404 file not found file when this error occurs.
  2. Password protection
  3. Image hotlink prevention.
  4. Most import is url rewriting

OpenLiteSpeed also supports .htaccess directives. However OpenLiteSpeed does not support some directives, you can learn more about that here. So if you are looking to get super fast websites you can install CyberPanel which ships with OpenLiteSpeed by default.

Why we need SEO optimization in .htaccess files?

Before we talk about the actual working of the configuration file, lets first get to know how does .htaccess file helps in SEO of websites. You can see the list below:

  1. With the .htaccess file, you can make user-friendly URL which is very easy for the Search engine to understand. In result, your website SEO score gets increased.
  2. Your Search Engin Crawls the way you set up the .httacess website functionality. if you have done it right Like your website have no broken pages or you navigate them to some other pages. It will also increase your website integrity.
  3. Your SEO performance gets increased if your .htaccess file resolved the 404 HTTPS error and handles the 301 redirects.

Creating SEO-Friendly URLs

Url plays a very important role to rank your website pages. According to Matt Cutts who worked as an engineer in Google, URLs structure plays a crucial role in ranking. He also mentioned how the keywords in the URLs add the value in ranking factor. For SEO-Friendly URLs you need to make the URLs length clean. If your URLs length will shorter it will easy for the user to remember them. Google always look for User-friendly URLs and to rank them.

You need to optimize the .htaccess file to deal with this type of issue. For that, you need to override the current URLs using file extension and ensure that the current URLs are handled properly. To do so, you can follow .htaccess 301 redirect guide.

Creating Content Management System URLs

For Content Management System URLs you have to customize your URLs differently. Most of CMS won’t allow customizing the URLs, you will able to find some restrictions on it. With the use of .htaccess file, you can add the clean URL’s very easily with the following code in .htaccess file.

RewriteEngine On

Rewrite Rule (.*)/$load_page.php?&page_id=$1

After adding the code in the file you just need to update the file and your clean URL’s will work fine and your SEO performance definitely gets better.

Removing .php and .html

You can also remove the .php and .html extensions from your page URLs. It adds no value for the reader. On the other hand, it makes difficult for the user to remember them. To remove the .php and .html extension from your URLs you just need to add the following code in .htaccess file:

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f   //Removes .php extension

RewriteCond %{REQUEST_FILENAME}.html -f  //Removes .html extension

Rewrite Rule ^(.*)$ $1.html

So that’s how we can remove the .php and .html extensions from our page URLs through .htaccess file for SEO quality.

Redirects Using .htaccess for SEO

404 is a common error that needs to be resolved by using redirects in the .htaccess file. As an SEO professional, you need to make sure that various redirects are placed to avoid 404 errors. By placing 301 redirects and 404 redirects in .htaccess file make sure that your user won’t land on a broken page.  It also helps the google crawler to ensure that your website doesn’t have any broken page and they rank your website good.

To do the redirect for all the errors including 400: bad request, 401: authorization required, 403: forbidden content, and 500: internal server error, you just need to use the content mentioned below.

ErrorDocument 402 /temp/402Error

The above code will work for any error. All you need to do is simply change the error code and also change the redirect page associated with it.

Latest Website Redirection

As you know, redirect the user to the latest version of your website is very important regarding the SEO better performance. Google index your website pages all the time. As a website owner, you never want a visitor to land on the older version of your website. You have to redirect the user to the new version of your website using 301 redirection in .htaccess file. This can be done by using the following code in your .htaccess file.

RewriteCond %{HTTP_Host} ^yoursite.com$ [NC]

RewriteCond ^(.*)$ http://www.yoursite.com/$1[R+301,L]

RewriteCond %{THE_REQUEST} ^.*/index

Rewriter ^(.*)index$ http://www.site.com/$1 [R=301, L]

Sending Spiders to Sitemap

For a well-ranking site on Google, search engine spiders need to be able to crawl the sitemap to index pages, posts, categories, and tags. If your website doesn’t allow for automatic regenerations of new sitemaps when your website is updated, add the appropriate .htaccess code. By adding an HTTP 301 Redirect, you’ll create a permanent redirect to tell search engine spiders exactly where the file is located to increase your ranking.

< IfModule mod_alias.c >

RedirectMatch 301 /sitemap.xml$ http://site.com/sitemap.xml

RedirectMatch 301 /sitemap.xml.gz$ http://site.com/sitemap.xml.gz

Improving the .htaccess for SEO With Site Speed Caching

In today’s competitive market, website speed matters the most. If you want to rank higher on the search engine, you need to improve the speed performance of your website by adjusting the cache in .htaccess file. Caching is one of the technique to make your website load faster. Caching is a method that stores resources in the browser. So when every time user visits your site it’s already stored in your browser and your website load time would be faster. By enabling caching, not only you speed up your website, but also save server processing time.

To enable caching, we are going to use mod_headers and mod_expires. These will give you a better way of handling the overall caching process.

Let’s get started.

<ifModule mod_headers.c>

ExpiresActive In

ExpiresDefault A259200

The above code will help you set an expiration time for assets. The number might seem significant as it is in seconds.

Configuring .htaccess Files

There are many advantages and disadvantages to editing .htaccess files. There are great benefits because the changes will be immediate to boost the functions of your site. But, if you don’t know what you’re doing, you’ll hurt the performance of your website and cause some serious damage. Not to mention, if it isn’t done correctly, it may cause security issues. If you aren’t sure what you’re doing, don’t touch it! It’s best to leave the server alone and hire a professional to configure .htaccess files correctly.

If doing all this is too much for you, better get a managed vps service and let experts do that for you.