Configuring Your Website’s .htaccess for SEO Purposes

htaccess-guide

For many websites, primarily those that aren’t based off a Content Management System (CMS), the .htaccess file is generally not present. However, a properly configured .htaccess file is part of good on-site SEO practice and can help to give you a bump in the search results. Below I’ll share with you some of the common .htaccess rules which are quite useful for SEO purposes.


Getting Started

Firstly, for your .htaccess file to work, you’re going to want to make sure to add the following to the top of your .htaccess file.

RewriteBase /
RewriteEngine On

Setting Up SEO Friendly URLs

This is primarily a technique to improve the attractiveness of URLs. If you have a URL such as:

  • https://www.some-website.com/random-page.html

You can make the URL look like the following instead.

  • https://www.some-website.com/random-page

The code to make this work is:

RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

If your website is built using .php or another format, just substitute .html for the respective format of your website.

However, for the above code to work, you need to rewrite all the links on your website. For example:

<a href=”randompage.html”>……</a>

Should become

<a href=”randompage”>……</a>

Setting Up Permanent Redirects

For older websites that have undergone major re-designs and restructuring, 301 redirects are very important to pass link juice to a new URL. To create a permanent 301 redirect to point one page to another, you can easily use the following code.

Redirect 301 /some-old/website-url https://www.example-website.com/new-url

Custom Error Pages

Setting up a .htaccess file is a great way to define custom error pages for your website. In the case of a 404 error page, this prevents bots from landing on a server based 404 error page like the one below.

404-server-error

With this type of error page, link juice can be lost since there are no links from this 404 error page to point back to the website.

To create custom error pages, use any one of the following lines of code for the respective error page you want setup.

ErrorDocument 400 400-error-page.html
ErrorDocument 401 401-error-page.html 
ErrorDocument 403 403-error-page.html 
ErrorDocument 404 404-error-page.html 
ErrorDocument 500 500-error-page.html

Defining your website to be www. vs non-www.

If you’re not using rel=”canonical” to define to Google which version of your website you’d like to have indexed, then there’s a chance that both the www. and non-www. version of your website are indexed in Google. To prevent duplicate indexing of your website, adding this to your .htaccess file will be very helpful.

Define Website to be www.

RewriteCond %{HTTP_HOST} ^example-website\.com$ [NC]
RewriteRule ^(.*)$ https://www.example-website.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index$ https://www.example-website.com/$1 [R=301,L]

Define Website to be non-www.

RewriteCond %{HTTP_HOST} ^www.example-website\.com$ [NC]
RewriteRule ^(.*)$ https://example-website.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index$ https://example-website.com/$1 [R=301,L]

Removing Trailing Slashes

Just like the example above, some servers may server both a non-slashed and a slashed version of a URL. As a result, you can end up  with two versions of the same web page being indexed. For example:

  • https://example-website.com/random-folder
  • https://example-website.com/random-folder/

This is issue is generally not very common. However, if you do have this issue, it can be easily solved by completely removing the trailing slash.

RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1
Andre

Andre is a Digital Marketing Specialist and the Founder of Thunder Rank. He has passion to learning, as well as sharing and developing effective online marketing strategies with the online community.

This entry has 0 replies

Comments are closed.