Best .htaccess Guide for SEOs (With Examples!)

Written by Dileep Thekkethil

Updated on Sep 17, 2020
Category: Technical SEO

Your website may have come down after you made a few edits on your .htaccess file. But that’s nothing to be worried about!

This happens with many SEOs who try to go the extra mile by doing technical fixes. 

Usually, the edits made through the WordPress dashboard give a mini panic attack as you’re locked from the dashboard once you update the erroneous .htaccess file.

.htaccess files are both a boon and bane for SEOs as it helps you to customize the functions of your website. 

Starting from protecting files with a password, doing a 301 redirect or setting up a special error page, .htaccess comes handy if your website runs on the Apache Web Server.

If you’re someone who is looking for a quick fix to get your website back live after a .htaccess update failure or if you want to learn more about optimizing the .htaccess of your website, this is the resource that you thought never existed. 

We are going to give you some very quick .htaccess fixes and tips so that you, as an SEO, can now confidently make changes to the website without worrying about the aftermath.  

You already know what .htaccess is and how useful it is for a webmaster who runs an Apache Web Server. 

However, I will explain a few basics of .htaccess for the beginners to help them understand it before they implement. 

Please feel free to toggle to the Chapter of your choice using the Table of Content. 

.htaccess example guide

What is .htaccess? 

.htaccess is a configuration file that can be read and executed by the Apache Web Server software to enable/disable additional functionality and features. Since the .htaccess file exists in a Unix-based environment and renders within the directory level, it overrides the global web server settings allowing custom configuration of website-access.

What does .htaccess stand for? 

.htaccess is an acronym for “hypertext access.” The name was derived after the file became popular among developers who used it to modify the user accessibility functions on a per-directory basis. 

.htaccess uses the Apache server’s http.config directives to allow and restrict access to directories for individuals using usernames and passwords. However, when it comes to SEOs, .htaccess has an even bigger role to play. 

What is .htaccess used for? 

If you have a website that comes up with different requirements on a consistent basis, consider .htaccess as a boon. 

As an SEO, .htaccess comes handy as you can use it to give directives to the web server to 301 redirects, enable caching, update HTTP headers, control crawling, make URLs SEO friendly and much more!

Where can you find the .htaccess file? 

Since .htaccess is a directory level configuration, you can find it in almost all folders within your web directory. If you have a single web directory with multiple subdirectories (website), the .htaccess file can be found in the root directory as well as inside each subdirectory. 

If you’re a WordPress user, the best way to access the .htaccess file is through the Yoast Plugin Option. 

Caution: Since .htaccess is not something that beginners should play around, I recommend testing it out on the demo site. Also, you must have ample knowledge about using Filezilla to access the root folder if you get locked out of the WordPress Admin. 

If you’re running a WordPress website, there are a handful of plugins that support editing the .htaccess file. Since most of the WordPress users choose Yoast as their default SEO plugin, let me explain how you can edit the .htaccess file in the Yoast Dashboard within WordPress. 

Step 1: Log in to your WordPress Admin Dashboard

Step 2: Open Yoast Settings

Step 3: Open Tools 

Step 4: Within the tools, select “File Editor”

Step 5: Edit the .htaccess file and save

In most of the WordPress websites, .htaccess comes with this default configuration: 

# BEGIN WordPress

RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ – [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

# END WordPress

What if your website doesn’t have a .htaccess file?

Since .htaccess is an automatically generated configuration file, in 99% cases, it will be present. However, it’s usually a hidden file within the directories and webmasters think that it’s missing. Just enable “show hidden files” and you should be able to find it. 

If you fail to find the .htaccess file even after doing this, you may have to write one manually and upload it to your web server. 

Steps to create a custom .htaccess file

Step 1: Open notepad

Step 2: Type down the configuration (to test use the default configuration provided above)

Step 3: Save the file in ASCII with file name .htaccess

Step 4: Ensure the file is not getting saved in .txt format

Step 5: Use Filezilla to upload the .htaccess file to your web directory

Note: If you’re a WordPress user and have an empty field for the .htaccess file, just add the configuration and save.

What to do if you get locked out of WordPress Dashboard after updating .htaccess file? 

You may have updated the .htaccess file using Yoast or using WP File Manager and got locked out of the dashboard. Don’t panic. This can be easily fixed.

If you’re an SEO, get the FTP access of your website from the developer on the one who manages the server. If you require the help of an expert, your developer could be the best helping hand. 

Step 1: Log in to your web directory using FileZilla 

Step 2: Download the .htaccess file from the directory

Step 3: Update the .htaccess file with the default configuration and save it

Step 4: Replace the old .htaccess file in the directory with the new one

Note: If your priority is to bring back the website online ASAP, use the default code. However, if you have made a lot of changes to the .htaccess file, try to find the code that backfired. Remove it before uploading it back to the server. 

Why are you seeing an error after updating the .htaccess file? 

If you’re an SEO trying to learn .htaccess in-depth, chances are you will end up making a few mistakes in the beginning. This is perfectly fine until you understand why the web server flagged an error. 

There are a few common problems that SEOs encounter when it comes to updating the .htaccess file. Here is the list of the most common .htaccess issues.

  1. Disabled Override: For your .htaccess file to function, you must first enable AllowOverride option. If the AllowOverride option is set to None, all configurations set in the .htaccess will be disabled. To ensure that the Override option is enabled, follow this step:

Step 1: Open the Apache Configuration File (http.conf)

Step 2: Set Allow OverRide directive as : AllowOverride All

Step 3: Save the Apache Config file and restart the Apache Server

  1. Misspelled File Name: This is the second most common mistake that SEOs make. Since .htaccess is a Unix-based configuration file saved in ASCII, misspelling it will result in an error. If the file does not start with a “.” or get uploaded in a different file format such as .txt, the Apache server will ignore the file and the configuration that you set. 
  2. Hierarchy of .htaccess Matters: .htaccess files execute based on their hierarchy. A few rules set at the beginning of the .htaccess files might annul the rules that come in the later stage of the configuration. If you think these are critical, try moving the particular configuration up. 
  3. Multiple .htaccess files: Since .htaccess files can be used per-directories, there’s a chance that your website is using multiple .htaccess files. In such cases, one file can contradict the configuration set in another, resulting in errors. You can troubleshoot this by disabling individual .htaccess files.
  4. Syntax Error: The .htaccess file functions completely based on the syntax that is used to configure your website. A syntax error on the file can take your site offline and cause a panic attack. So it’s imperative to understand the syntax before updating your .htaccess files. 

What are the common error messages that you get after updating the .htaccess file? 

Each time a user visits your website, he/she is interacting with the web server directly or indirectly. The pages that they get on clicking the images and other resources that they see are fetched by the web server, which means you as the webmaster can restrict it. 

Some websites use .htaccess to set authentication for accessing websites. When it comes to SEOs, they use it to ensure that the users and search engine bots get easy access to pages that matter the most. If the website fails to provide the requested information, an error code is generated by the web server based on the configurations set in the .htaccess file. 

Here is the list of error codes that web servers display once they fail to fetch the requested data.

Client Request Errors

  • 400 — Bad Request: Invalid URL Structure. The server is unable to understand the request raised by the user. 
  • 401 — Authorization Required: These messages get displayed when the access to the page has been restricted by the webmasters. 
  • 402 — Payment Required (not used yet): If the payment initiation fails to happen, it generally gives this code.
  • 403 — Forbidden – The simple reason you get a 403 error is because you’re trying to access a resource that has restricted permission. A website displays 403 forbidden errors when the users try to access a page that requires authentication. 
  • 404 — Not Found: 404 is a clear indication to the users that the requested URL is not available on the website. It can be because of a typo error in the URL or when the page has been removed from the site.
  • 405 — Method Not Allowed: This HTTP response status code indicates that the server has refused to accept the request method despite understanding the purpose of the request. 
  • 406 — Not Acceptable (encoding): This usually happens when the server cannot respond with the accept-header request.
  • 407 — Proxy Authentication Required: This error indicates that the request cannot be fulfilled because of the lack of proxy server authentication between the browser and the server. 
  • 408 — Request Timed Out: This is one of the common HTTP errors that webmasters encounter when the server fails to receive a complete request from the client side within the allotted timeout period.
  • 409 — Conflicting Request: This error happens when the state of the target resource conflicts with the current state. To resolve the error, identify the conflict and resubmit. 
  • 410 — Gone: This error code represents that the access to the requested resource has been permanently removed from the server and it will remain so throughout.
  • 411 — Content Length Required: The error represents the inability of the server to accept the client request due to the failure to define the content-length header.
  • 412 — Precondition Failed: The is an error caused due to the security clash with one or many of the security configurations that have been implemented on your server. 
  • 413 — Request Entity Too Long: When the requested resource is too large for the server to load, the user may experience the 413 error. 
  • 414 — Request URI Too Long: Just think of a URL structure above 2048 characters. The server cannot decipher the resulting 414 error. 
  • 415 — Unsupported Media Type:  This error shows up when the server refuses to load a resource that is in an unsupported media format. 

Server Errors

  • 500 — Internal Server Error
  • 501 — Not Implemented
  • 502 — Bad Gateway
  • 503 — Service Unavailable
  • 504 — Gateway Timeout
  • 505 — HTTP Version Not Supported.

What is .htaccess used for? 

Redirects

Did you just change the domain name of your website? 

As an SEO, you definitely don’t want your users to see a 404 page, nor do you want all the hard-earned authority to just vanish. 

Consider your .htaccess as the panacea for both these concerns. Adding the redirect directive to your .htaccess file will help you to redirect your traffic and site authority to the new domain.

The interesting thing is you can do the same when it comes to the URLs within your website. By creating a 301 redirect directive, you push your users and search engine bots trying to access an old URL to read a new page within your site. 

Example for Domain Level Redirect with .htaccess: 

# This allows you to redirect your entire website to any other domain 

Redirect 301 / http://example.com/

Example for URL Redirect using .htaccess: 

RedirectMatch 301 ^/old-url\.html$ /new-url.html

SEO-friendly URLs

Is your URL structure messed up? Are users and search engine bots unable to understand what is within the page? This happens to quite a lot of webmasters. 

The failure to give attention to the URL structure initially becomes a big pain after the website gains authority. 

Using the .htacces, you can set the proper URL structure for your website. 

In addition to this, any extension that comes along with your URL, for example, .html or .php can be easily removed by adding directives to the .htaccess. 

Example 1: Using .htaccess to remove extensions in the URL

Removing .php as Extension

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)$ $1.php [NC,L]

Removing .html as Extension

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.html -f

RewriteRule ^(.*)$ $1.html [NC,L]

Example 2: Using .htaccess to make URLs lowercase

Method 1

RewriteCond %{REQUEST_URI} [A-Z]

RewriteRule . ${lc:%{REQUEST_URI}} [R=301,L]

The above code will impact all URL structures, including the domain name.

Method 2

RewriteCond %{REQUEST_URI} [A-Z]

RewriteRule ^.+\.html$ ${lc:%{REQUEST_URI}} [NC,R=301,L]

Above one will impact only html file names.

Example 2: Using .htaccess for Dynamic URL rewriting 

RewriteEngine On

RewriteRule /(.*)/(.*)/$ page.php?category=$1&product=$2

Bad URL: site.com/page.php?category=2&product=54

Good URL:  site.com/sandwiches/rueben-sandwich/

  • Using .htaccess to Improve Site Speed

Page Speed is the new hoo-ha among webmasters.

They have all the reasons to create the commotion as Google now considers it as one of the major factors while ranking pages on the SERP. 

Google doesn’t want the users to have a bad user experience from the top listed pages. 

Additionally, a slow loading website eats up a lot of crawl budget. 

One of the easiest and safest ways to improve the speed of the website is by configuring your .htaccess file. 

  • Enabling Cache Feature

By enabling the cache feature in the .htaccess file, the resources of the website get stored on the visitor’s browser enabling quick load. 

You can enable cache using two different methods 

  • ExperiesByType – Using this method in .htaccess, you can set a cache time frame, aka expiry for each resource within your website. 

Example: 

<ifModule mod_headers.c>

# YEAR

<FilesMatch “\.(ico|gif|jpg|jpeg|png|flv|pdf)$”>

  Header set Cache-Control “max-age=29030400”

</FilesMatch>

# WEEK

<FilesMatch “\.(js|css|swf)$”>

  Header set Cache-Control “max-age=604800”

</FilesMatch>

# 45 MIN

<FilesMatch “\.(html|htm|txt)$”>

  Header set Cache-Control “max-age=2700”

</FilesMatch>

</ifModule>

  • Cache-Control Header – Cache-control header uses maximum age for the resources before expiring. 

Example: 

# One month for most static assets

<filesMatch “.(css|jpg|jpeg|png|gif|js|ico)$”>

Header set Cache-Control “max-age=2628000, public”

</filesMatch>

Enabling Gzip Compression

The one reason that deteriorates the speed of a website is the size of its resources. By enabling gzip in your .htaccess file, you can reduce the image size, file size, and file quantity while sending data to the client side browser. 

Enabling gzip is one of the easiest .htaccess directives. 

Here is an example: 

<ifModule mod_gzip.c>

mod_gzip_on Yes

mod_gzip_dechunk Yes

mod_gzip_item_include file .(html?|txt|css|js|php|pl)$

mod_gzip_item_include handler ^cgi-script$

mod_gzip_item_include mime ^text/.*

mod_gzip_item_include mime ^application/x-javascript.*

mod_gzip_item_exclude mime ^image/.*

mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

</ifModule>

Enabling Deflate Option

Some web servers don’t support gzip, so the website might run into errors. In such cases, it’s advised to use the deflate option in the .htaccess file. 

Here is an example: 

<IfModule mod_deflate.c>

    # Compress text, HTML, JavaScript, CSS, XML

    AddOutputFilterByType DEFLATE application/javascript

    AddOutputFilterByType DEFLATE application/rss+xml

    AddOutputFilterByType DEFLATE application/x-font

    AddOutputFilterByType DEFLATE application/x-font-opentype

    AddOutputFilterByType DEFLATE application/x-font-otf

    AddOutputFilterByType DEFLATE application/x-font-truetype

    AddOutputFilterByType DEFLATE application/x-font-ttf

    AddOutputFilterByType DEFLATE application/x-javascript

    AddOutputFilterByType DEFLATE application/xhtml+xml

    AddOutputFilterByType DEFLATE application/xml

    AddOutputFilterByType DEFLATE font/otf

    AddOutputFilterByType DEFLATE font/ttf

    AddOutputFilterByType DEFLATE image/svg+xml

    AddOutputFilterByType DEFLATE image/x-icon

    AddOutputFilterByType DEFLATE text/css

    AddOutputFilterByType DEFLATE text/html

    AddOutputFilterByType DEFLATE text/plain

    AddOutputFilterByType DEFLATE text/xml

    # The following lines are to avoid bugs with some browsers

    BrowserMatch ^Mozilla/4 gzip-only-text/html

    BrowserMatch ^Mozilla/4\.0[678] no-gzip

    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

    BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

    # Do not cache if these files are already cached

    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip

# Proxies must give the right content

# Header append Vary User-Agent env=!dont-vary

    Header append Vary User-Agent

</IfModule>

Using .htaccess for Improved Crawling and Indexing

You may already be using the robot.txt file to allow and disallow the search engines from crawling and indexing your website. However, it’s possible that you have resources other than a webpage on your site. In such cases, the rbot.txt may not work. 

If you want a few resources such as a PFD or a Word Document to be no-indexed, the best way to do it is by setting up an X-robots-tag in your .htaccess file. 

The custom header of the .htaccess file can function with all indexer directives. 

Example: 

<FilesMatch “.(docx|pdf)$”>

    Header add X-robots-tag “noindex, noarchive, nosnippet”

</FilesMatch>

In this case, any file with .doc and .pdf extensions will be considered as noindex, noarchive, nosnippet. 

Conclusion

Almost all Apache servers have a preset configuration file. However, this applies to the whole site and so it’s hard to set directory level configuration. 

That is where .htaccess comes as a blessing. You can set directory and subdirectory level configuration using the .htaccess to override the Apache configuration settings. 

In addition to this, you can use simple configuration codes to set authentication. This is more useful if you have a shared hosting with multiple websites. 

Advantages of .htaccess file

  • It reads all requests
  • Immediate change without restarting the server
  • Effectively manage the user access based on the preference
  • Sets directory level configuration
  • A real boon for SEOs

Disadvantages of .htaccess file

  • .htaccess files can increase the security risks of running a site
  • Slower than server level configuration as the .htaccess is searched and read every time a page loads
  • It impacts the speed of a website, which drives traffic in millions. 

.htaccess files are not recommended as a method of server configuration due to security and performance problems.

About Dileep Thekkethil

Dileep Thekkethil, a Journalism Postgraduate, was formerly with a US-based online magazine, is the SEO Expert at Stan Ventures. A He is a frequent blogger who keeps a tab on the latest updates in SEO and technology arena. Reach me @ Mail | LinkedIn | Twitter | Facebook or View all posts by Dileep

You May Also Like…

2 Comments

  1. Avatar

    Hi Dileep,
    so without any redirection we can remove extensions from the URL using .htaccess is that what are you saying?

    Reply
    • Avatar

      Hi Herman,

      Yes.

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *