Best .htaccess Guide for SEOs (With Examples!)

By Raghav Tayal

Websites are made up of many tricky components and need optimization beyond their content and on-page SEO. On-page SEO and content optimization play a crucial role in increasing the visibility of a given website in the SERPs, helping bring more traffic and much more. But the job is only half done in the absence of tweaking the more technical elements comprising the website and its structure.

That said, another equally vital part of the big picture that is SEO is technical search engine optimization. Technical SEO is concerned with improving the overall navigability and other technical issues of a website, such as its page speed, sitemaps, schema, structure, and more.

Many website owners often end up overlooking technical SEO, which dents their sincere attempts at content optimization and other elements of on-page SEO. What exactly is technical SEO, you may ask?

Simply put, technical SEO is essentially the process of optimizing the website elements we just mentioned. A more comprehensive definition of technical SEO says that it is the exercise that optimizes the infrastructure of a website to make it suitable for the search engines to index and browse through it.

If you aim to attract a higher amount of organic traffic to your website, you must pay close attention to technical SEO. Technical SEO adds to your efforts of creating valuable and relevant content along with the other design elements of your website.

Technical SEO, when done right, works wonders in making your website mobile-friendly, highly functional, faster, and, more importantly, super easy to navigate. It adds heft to the overall looks and feels of your website that you painstakingly designed to be noticed by your target audience.

Technical SEO involves the use of many tools. One such tool is a .htaccess file.

Do you want to learn more about it? Keep reading.

In this article, we will explain what is a htaccess file? We will talk about how to use the .htaccess file, with relevant examples. We will give you an htaccess guide to help incorporate the use of this powerful tool in customizing your website and its server. We will explain how this file will affect the functionality of your website and try to give a better understanding of its scope. So without further ado, let’s learn some htaccess basics.

What is .htaccess?

A .htaccess(short for ‘hypertext access’) file is a developer tool that can be used to customize various functions and the overall performance of your website. It works specifically for the sites whose web servers run on the Apache Web Server software.

A .htaccess file is one of the most effective and powerful tools in the technical SEO process. An average person doesn’t notice its impact and effect on their browsing experience, but it can do wonders for your SEO because of its versatile functionality.

The Apache web server is a massive player in the internet realm. It is an open-source software developed by the Apache Software Foundation. It is an HTTP server, meaning that it is used to host web content.

Note that a .htaccess file also runs on a few other servers like LiteSpeed. But for the sake of our discussion here, we will not talk about LiteSpeed or those other servers.
How does a web server work you may ask?

Its work begins when a user requests a website by entering the web address or URL of the given website in the address bar of a browser. In turn, the browser sends a signal request to the web for displaying the corresponding webpage.

Here, a Domain Name Server(DNS) comes into the picture and converts the URL entered by the user to its IP Address, which looks something like this: 192.158. 1.38. All websites on the internet have their unique identifier, its Internet Protocol address, that points to the webserver where they are stored.

Simply put, a web server works through communication between the user and their URL request via a translation provided by the IP address corresponding to that URL.

Now, coming back to the Apache Web Server. It is easily the most commonly used web server in the market. You can only understand the magnitude of its relevance in the backdrop of knowing that almost 70 percent of all websites on the internet today are handled by the Apache Web Server software.

This information should be sufficient to dawn on you the importance of knowing the usability of a .htaccess file. Now, we can dive deeper into its definition and use cases.

What is a .htaccess file exactly?

In more technical terms, the access definition says that it is a distributed server configuration file that is used on the apache web server software. It is a subset of Apache’s directory level web server configuration rules. The filename here is htaccess, and the period(dot) before it keeps it hidden within a folder.

A configuration file, or config file, defines the initial settings, parameters, preferences, and options to a software application. It contains the data of a particular user, computer, program, or file.

It can configure a server for the directory that contains the .htaccess file. The Apache software detects and executes a .htaccess file when it is stored in a directory that is present on the Apache web server.

In this series, a .htaccess file can be used to extract additional features and functionality of the apache web server software. It acts like a weapon to bring out the best of the software to help impact your technical SEO positively.

Similarly, it can also be used as a customization tool to tweak the environment and functionality of a website. You can make changes to the configuration of your website(given that it is stored on a server that runs Apache) without going through the arduous process of editing the configuration files on the server.

Where is a .htaccess file located?

You won’t find a .htaccess file in plain sight as Apache has a default configuration that keeps these files hidden. The reason for keeping these files hidden is the content they store within them. These files are vital to the security of a web server because they contain vital configuration information. Any lapse in ensuring its safety may lead to compromising the entire server.

That said, you can most commonly locate the .htaccess file for a given website in its public_html folder. How can you access this file then? Well, there are a few ways to do so. These include:

  • Through an FTP(file transfer protocol) or an sFTP(secured file transfer protocol) client
  • Through the file management section on your web hosting account (Hostinger, CPanel, UpCLoud, etc.)

In case the file is not visible in the public_html folder, you need to check the option “show hidden files”.

How does an .htaccess file work?

The .htaccess file for a website configures a web server for the given website. Commonly, the .htaccess file for a website is placed in its root directory, for example, /public_html. All the different directories of a website are stored on a web server.

There are instances where the .htaccess file can be placed in another sub-directory. But the practice is only followed for some very specific purposes that are beyond the scope of this article.

In our case, the root directory, like the other directories of the website in question, is stored on the Apache webserver. Whenever a request invokes the root directory(or any subdirectory, in case the .ht access file is located there), the Apache webserver executes the .htaccess file. This is how the server is configured for the entire website with the help of the .htaccess file.

The root directory is the central directory of any given website.

The Apache software uses the top to bottom approach for reading all configuration files. This approach means that Apache executes the files on the top first, gradually proceeding downwards.

Understanding the structure of an .htaccess file

Before you dive into the use cases of this versatile file, you must become familiar with the syntax and structure of .htaccess. We will be learning more about its structure by taking an example of a basic WordPress .htaccess file. You might likely have seen this before and pondered about its significance. After today, you will not be clueless about it when you see it next.

Following is the default configuration of a .htaccess file on a normal WordPress website:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Remember that the order of code in .htaccess is of importance. It’s vital that each step is carefully placed in the order you want to execute something.

Now, let’s break down the code line by line.

  • RewriteEngine OnThis line of code ensures that the rewrite engine from Apache is turned on for the given site. Another way to switch on this engine is on the server level. You can set it in the global conf file httpd.conf. You won’t need to repeat the command here if the engine is set on the server level.
  • RewriteBase /We need to set a base for when further rewrite rules have to be created at a later stage. Setting a base is necessary to be able to use relative paths. The / character here signifies the root of the document the files for the website are located. For example, if we have a website ‘abcxyz’, the / means the same as https://www.abcxyz.com/ after the translation to the domain URL.
  • RewriteRule ^index\.php$ – [L]There’s no limit for having rewriting rules. These rules act as the backbone for the rewriting process. You can virtually have an endless number of rewrite rules. These rules are checked multiple times during every pageview on the website in question.Rewrite rule syntax is:RewriteRule Pattern Target/Substitution [Flag1, Flag2, Flag3]

    Here, the ^index\.php$ is an expression that helps find a group of strings. The ^ symbolizes the start of a string. Similarly, $ is used to mark the end of the same string. For strings that have a dot in them, you can use the / symbol in place of ^. In this case, this line of code provides just one exact match which will be for index.php.

    The – here is used to view the substitution or the target. The – signifies that there is no substitution in this line of code. [L] acts as a flag that asks to stop processing the rules.

  • RewriteCond %{REQUEST_FILENAME} !-fRewriteCond stands for Rewrite Conditions. It is used for restricting the requests affected by rewrite rules that follow these conditions.The syntax for rewrite conditions is:RewriteCond Teststring Condition [Flag1, Flag2, Flag3]

    Multiple conditions mandate all of them to be true before the application of any other rule. In the case of our code here, the condition is !-f. -f stands for “is regular file”. It checks if the Teststring is a valid file or not. ! stands for ‘not’. Thus, here, this line checks if the Teststring is not a valid file.

    Here, the line checks whether the complete URL being accessed by a visitor is a valid file or not. The idea behind this line of code is to catch and identify various rewrite oaths. If you don’t wish the server to keep returning 404 errors, this line of code processes the permalinks.

  • RewriteCond %{REQUEST_FILENAME} !-dInstead of checking whether a file is valid, !-d expression checks whether the directory being accessed is valid or not. Only a clear match leads to the processing of the next rule in line.
  • RewriteRule . /index.php [L]Once all the conditions of the code are satisfied, we use this rule. It is used when a user tries to access anything on the given website which doesn’t exist, to begin with. The dot signifies any character other than a line break. The target, in this case, is /index.php. It is the index.php file that we had earlier protected from rewriting. The [L] ceases the processing of any rules that might follow.

Creating a custom .htaccess file

You can create your own .htaccess file by simply opening the notepad. The straps that follow are:

    1. 1. Enter the configuration you want.
    2. 2. Next, you will be required to save this file in ASCII with a file name called .htaccess.
    3. 3. A file saved in .txt won’t solve the purpose. Therefore, you need to make sure that the file is not getting saved with .txt.
    4. 4. Use an FTP application such as Transmit, Filezilla, or any other to upload the final .htaccess file to your desired web directory.

Role Played By .htaccess Files In Improving Technical SEO

The idea behind configuring the files on the main server using .htaccess files is to directly boost your SEO performance. It results in signaling the search engine spiders to take more notice of your website and increase its overall reach and functionality.

You can replace the standard files with the ones you have designed intending to create cleaner and better URLs, produce more effective 301 redirects, boost your website’s cache, resolve error codes such as the 404 status codes, and so much more.

Some of the most widely used cases for .htaccess file for boosting a website’s SEO include:

      • It can be used to allow or deny access to certain visitors.
      • Helps prevent directory listing, i.e., it stops users from viewing the files present in an internal directory structure.
      • Authorization and authentication: Helps boost security through password protection.
      • It helps improve the website speed in various ways, including compression.
      • It helps redirect the pages that return error signals or have moved to a different location.
      • It helps block unnecessary and malicious bots and traffic as and when necessary.
      • It helps provide nicely themed messages to the visitors for errors. Sometimes errors are inevitable and are always helpful in informing the users about exactly what is happening.
      • It also plays a crucial role in implementing HSTS(HTTP Strict Transport Security) and normal HTTP. The implementation on the Apache server fails if the syntax has any incorrect header information or incorrect URL rewriting.
      • It can aid in writing SEO-friendly and easy-to-read URLs.
      • It helps prevent hotlinking to prevent your website’s bandwidth from being used unnecessarily.

Allow/Deny Access

Spam visitors on your website may indicate a higher traffic number in your analytics data. Spam visitors skew the actual data and end up confusing you in your pursuit to use the data for different purposes. You can recognize potential spam visitors. Once you have identified them, you can use .htaccess to deny them access to your website or its parts.

You can ban spam visitors from your website using their IP address. For example:

We can use the following .htaccess file code to allow access to just a single IP address 5.6.7.8 :

# Order Allow, Deny
Deny from All
Allow from 5.6.7.8

The keyword ‘order’ has been used to specify the processing order of allowing/denying access. In this example, the Allow statement will be processed before the deny statement.

Similarly, you can deny access to the same single IP address using the following code:

# Order Allow, Deny
Deny from 1.2.3.4
Deny from 1.2.3.5
Allow from All

You can also deny access to hidden files or directories by using .htaccess. It’s only natural that you would want your website’s hidden files to be kept away from the reach of visitors with malicious intent or otherwise. For example:

RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule “(^|/)\.” – [F]

A simple alternative to the above code is:

RedirectMatch 404 /\..*$

This is a simple ‘Not Found’ error.

You can also use .htaccess to deny access to source files and backup files. You cannot afford to compromise on these files because that could endanger your website and your business. You have to protect these files from being available in the public domain. An example code is:

<FilesMatch “(\.(bak|config|dist|fla|inc|ini|log|psd|sh|sql|swp)|~)$”>
Order allow,deny
Deny from all
Satisfy All

Improving Indexing/Crawling

A website that gets crawled more because of the ease of crawling can be indexed more easily. When the search bots find it easier to crawl through a website, they give the website higher priority while deciding page rankings. Therefore, you can boost your website’s SEO considerably by improving the crawl ability of your website.

The .htaccess files can help you achieve this goal. There are two easy ways in which you can use .htaccess to improve crawling, and as a result, indexing. These are:

      • By Keeping The googlebot Focused
      • By Putting rel=”canonical On Non-HTML resources
      1. Keep the googlebot focused using .htaccessThe googlebot works based on two kinds of directives:
        • Crawler DirectiveThe crawler directive is used to point the Googlebot in the direction you want it to look on your website. You can also use it to tell Googlebot where your sitemap is. You can better inform the search engines to identify a website’s areas they should be crawling.The crawler directives are placed in the robots.txt file. Some common crawling directives include User-agent, Sitemap, Disallow, Allow, etc.
        • Indexer DirectiveAs the name suggests, the indexer directive informs the Googlebot about the content that they should index on a given page on your site. These directives are placed on each element or page of the website. More precisely, they are placed in the HTML head of a given page. For example:Alternatively, they can be placed inside a link as well, for example:

          example page

          Robots.txt is a vital tool for any website, no matter its size. Generally, robots.txt provides directives to a search engine about the pages, folders, files, or subdomains that should be crawled on your website. Although, we have already mentioned how indexer directives are not placed using robots.txt.

          Now, coming back to indexer directives. When we talk about pdf files or images, they don’t have their HTML heads. In such a case, people will keep linking to these when you would not want them to. There is an option to use nofollow or noindex all those links that point to a pdf or image, but our problem would still keep lingering.

          This is where the .htaccess file comes into the picture. You can create a custom header called the X-robots-tag. You can use this tag with any given indexer directive.

          Handling .htaccess files are something that should be handled with care because it can have some significant repercussions if gone wrong. In the same sequence, using the X-robots-tag requires ample precautions. Undoubtedly it is an indispensable tool in technical SEO, but careful treading will be our advice to you.

          You can set an X-robots-tag just like you would a regular HTTP header. When dealing with the .pdf or .docx files, you can use code like this:

          <FilesMatch “.(docx|pdf)$”>
          Header add X-robots-tag “noindex, noarchive, nosnippet”

          Alternatively, you can also set it for single files using a code like:

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

      2. Put rel=”canonical” on non-HTML resourcesThe rel=”canonical” tag serves a valuable role in technical SEO. But before we understand that role, we should understand something about canonical tags. A canonical tag cannot be inserted into non-HTML pages on your website, the pages that have images, or PDF files. It can only be inserted within an HTML head.We can solve this problem by creating rel=”canonical” tags for the pdf files and images. Search engines allow the option to place a canonical tag as an HTTP header for images and pdf files.Let’s take a look at the process of creating a custom HTTP header.

        We begin the syntax for creating a custom HTTP header by invoking or tags. The opening or tag should contain the file name for identifying the file or a regular expression. These must be kept within quotation marks. In the case of regular expressions, ~ is used before it.

        Matching a file is followed by creating the HTTP header. The rules dictate that you use to match with the regular expressions and in case of file names.

        The syntax for creating an HTTP header looks like this: Header NAME “VALUE”

        There’s no need to place a semicolon after NAME in the .htaccess file.

        The following code is the example of how you may create a canonical tag that points to an HTML page of your choice for a single pdf file called random-file.pdf:

        Header add Link ‘<http://www.example.com/white-paper-download.html>; rel=”canonical”‘

        In case you want to create a dynamic HTTP header for each pdf that points to an HTML page containing the same name, the syntax can be something like:

        RewriteRule ([^/]+)\.pdf$ – [E=FILENAME:$1]
        <FilesMatch “\.pdf$”>
        Header add Link ‘<http://www.example.com/download/%{FILENAME}e>; rel=”canonical”‘

        You should be extremely careful while using this procedure. You should take note of the following rules:

        • You should use hyphens to separate words in the pdf file names and only use lowercase letters for names.
        • Never make the mistake of using this process in the .htaccess file of the root directory.
        • Check if each canonical link works. You can enter the same in a browser to do so.
        • Always make it a point to create the canonical HTML page before you upload the pdf document.

Tightening Security with Password Protection for Directories

It’s only natural that you would want to keep your website away from public viewing while it’s in the development stage. Even when the website goes live for the public, there are directories or pages that you would want to be locked. Password protection helps you achieve these goals.

You can use password protection to prevent the crucial pages, folders, or files of your website. It helps keep the administrative areas of your website safe and secure. The .htaccess file helps in password-protecting directories. The steps to create passwords for directories, other components, or the entire website using the .htaccess file are as follows:

Decide the directory where you would want to use password protection. Next, you need to create the .htaccess file. When you sue the main instructions, the text will look like this:

AuthName “Member’s Area Name”
AuthUserFile /path/to/password/file/.htpasswd
AuthType Basic
require valid-user

Here, the first line informs Apache that our secured directory is named ‘Member’s Area Name’. The second line specifies the password file’s location. The third line specifies the type of authentication. ‘Basic’ here means that the normal HTTP authentication type is being used. The fourth line requires the user to enter the login credentials.

1. You can place the password file anywhere on the webserver. But ensure that you have created a .htpasswd file. Note that .htpasswd file can have any customized name. The idea behind using .htpasswd file is to allow the server to identify it and thus, keep it hidden from the users.

2. Check whether the server you are working on requires you to locate the password file in the same directory that contains the .htaccess file.

3. A relative path or any URL variation for the password file will not work. You must ensure that you are using the full server path for the password file’s location. The text within a password may look like this:

Username:encryptedpassword
fred_smith:oCF9Pam/MXJg2

You can learn more about the password protection process here.

A real world example of using the password protection method is:

 

Order Deny,Allow

Deny from All

Satisfy Any

AuthName “Protected By AskApache”
AuthUserFile /web/askapache.com/.htpasswda1
AuthType Basic
Require valid-user

Here, the above code snippet will apply user authentication for someone who tries to access the WordPress wp-login.php file.

The password protection feature of the .htaccess file can help improve your technical SEO by providing an added layer of security.

Preventing Directory Listing

An open directory is set up for default browsing. Thus, any user will be able to view all the files, folders, and documents within an internal directory structure when the directory is open. Directory listing can compromise important files and folders or any other confidential information. You can use the .htaccess file to prevent directory listing.

It only requires a short and simple code snippet:

Options –Indexes

This is one of the easier .htaccess rules. Conversely, if you decide to enable directory listing, the code snippet for the same looks like this:

Options +Indexes

All you need to do is add one of these code snippets in the directory in question for hiding and displaying directory listing, as required.

Improving Page Speed

Other than navigability, the site speed of a website forms one of the most crucial areas to be addressed from the technical SEO point of view. It’s crucial to ensure a fast-paced website because a slow one will end up eating through your crawl budget. The crawl budget is the number of pages crawled by the searchbot on a website on any given day.

One of the key features of the .htaccess file is to improve the speed of your website. There are a few methods to increase site speed using the .htaccess file:

    1. Compressing the site (To Enable Gzip Compression)Logically, you can increase the speed of your website when it is smaller in size. You have the option to decrease the size of the files your website sends the client browser’s way. You can do this in addition to compressing the file size, images size, and quantity of files on your website. Now, when you compress the files sent to the client browser, you automatically increase the data transfer speed.You can reduce the size of the resources on your website by enabling the gzip in the .htaccess file. An example of the same is as follows:mod_gzip_on Yes
      mod_gzip_dechunk Yes
      mod_gzip_item_include file .(html?|txt|css|js|php)$
      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.*

      One caveat with gzips is the inability of some servers to process it without errors. In case errors appear, you can rely on the deflate method instead. An example of the same is:

      AddOutputFilterByType DEFLATE text/plain
      AddOutputFilterByType DEFLATE text/html
      AddOutputFilterByType DEFLATE text/xml
      AddOutputFilterByType DEFLATE text/css
      AddOutputFilterByType DEFLATE application/xml
      AddOutputFilterByType DEFLATE application/xhtml+xml
      AddOutputFilterByType DEFLATE application/rss+xml
      AddOutputFilterByType DEFLATE application/javascript
      AddOutputFilterByType DEFLATE application/x-javascript

    2. Using browser cachingBrowser caching is used to maintain the server load and the bandwidth for your website for repeat visitors. In this process, the resources from your website get stored on a visitor’s device.The cached copy of the saved resource, like an image file, for instance, is pulled from the cache memory of their device on a revisit. In this way, caching is a great way to reduce the extra load on the server. As a result, you can increase the speed of your website and positively impact its technical SEO.You can use browser caching in two ways. You can either use the Cache-Control header or simply set a cache timeframe for different resource types using ExpiresBy Type.

      Cache-Control

      You can declare custom headers for different resources. All you need to do is use the Header directive. Set a Cache-Control HTTP Header drawing from the following example:

      <filesMatch “.(css|jpg|jpeg|png|gif|js|ico)$”>
      Header set Cache-Control “max-age=2592000, public”

      Hre, max-age=2592000 is the time in seconds taken to cache a file. The browser is informed of the same from this code. If you convert 2592000 this in days, it comes to 30 days.

      ExpiresByType

      You can simply implement browser caching using ExpiresByType right before the resource type and its cache time period. For example:

      ##### Optimize default expiration time – BEGIN
      <IfModule mod_expires.c>
      ## Enable expiration control
      ExpiresActive On

      ## CSS and JS expiration: 1 week after request
      ExpiresByType text/css “now plus 1 week”
      ExpiresByType application/javascript “now plus 1 week”
      ExpiresByType application/x-javascript “now plus 1 week”

      Image files expiration: 1 month after request
      ExpiresByType image/bmp “now plus 1 month”
      ExpiresByType image/gif “now plus 1 month”
      ExpiresByType image/jpeg “now plus 1 month”
      ExpiresByType image/jp2 “now plus 1 month”
      ExpiresByType image/pipeg “now plus 1 month”
      ExpiresByType image/png “now plus 1 month”
      ExpiresByType image/svg+xml “now plus 1 month”
      ExpiresByType image/tiff “now plus 1 month”
      ExpiresByType image/x-icon “now plus 1 month”
      ExpiresByType image/ico “now plus 1 month”
      ExpiresByType image/icon “now plus 1 month”
      ExpiresByType text/ico “now plus 1 month”
      ExpiresByType application/ico “now plus 1 month”

      ExpiresByType image/vnd.wap.wbmp “now plus 1 month”

      Similarly, you can modify the code to extend the cache expiration time for different file types, including audio files, video files, font files, and more.

      The time frame is used to inform the user’s browser of the time before it can discard the cached copy of a given resource. Unless, of course, the user manually clears the cache on their device browser.

Creating Custom Error Pages

You can use the .htaccess file to create customized error pages for visitors. There are many instances when the server returns an error message for a particular client request. You can share more useful custom messages with visitors where you guide them back where they wanted to reach.

The standard error messages like “401 Unauthorized Access”, “404 File Not Found” or “500 Internal Server Error”, etc., may sound alarming and scary. You can create custom HTML pages for each of the standard error types.

You should make it a point to store these custom HTML pages in a directory called error_pages inside your website’s root directory. It is advisable to name the individual HTML pages with the error code corresponding to them, i.e., 401, 404, 500, etc., followed by the .html extension.

You can implement these HTML pages by adding them to your .htaccess file. For example, in case of HTML pages for error codes 404, 401, and 500 as seen here, add the following code to the .htaccess file:

ErrorDocument 401 /error_pages/404.html
ErrorDocument 404 /error_pages/401.html
ErrorDocument 500 /error_pages/500.html

Redirects

You need to be wary of losing out on your visitors and page authority whenever you end up shifting your website’s domain name. You don’t want the website visitors to find a 404 error message when they try to reach one of the pages on your website.

The .htaccess file can help you take care of such concerns with its ability to redirect the site authority and traffic to the new domain. You can also redirect URLs within your website. The latter can be done using the 301 redirects.

A 301 redirect sends search bots and visitors trying to access an old URL to the new location of that content on your website. The .htaccess file can direct from any given relative path within your site.

The URL can be changed using the .htaccess directives in two ways, namely, the mod_rewrite engine and the Redirect command.

The redirect command informs the browser about the other URL it should look for. Whereas the mod_rewrite tool translates the URL within a request. The idea is that the mod_rewrite engine converts the URL into a form that can be easily understood by a CMS or the file system.

The basic syntax in the .htaccess file for redirects looks like this:

Redirect 301 /relative-url.html http://newwesbite.com/full-url.html

As you can see, there are four parts to this code, and each of them are separated by a space.

The four parts are:

  1. 1. First is the Redirect command
  2. 2. 301 is the type of redirection we want here, i.e., a permanent move
  3. 3. The next part is the relative URL of the original page
  4. 4. And the fourth and the last part is the full URL of the new page

Here, the directory that contains the .htaccess file, generally the root of the domain, is relative to the relative URL.

Redirecting a complete website using .htaccess
You can move an a complete website to a new URL using .htaccess with the following code:

Redirect 301 / http://thenewurl.com

Redirecting a section of a website using .htaccess

You can also redirect all incoming requests from a particular directory on your website to a different directory. But before redirecting, note that you should make the relevant changes to the directory structure, like page names, etc. The code you can use looks like this:

Redirect 301 /old-directory http://forinstance.com/new-directory

Redirecting to www using .htaccess

You can ensure that users reach your www subdomain website when they are looking for some content you can offer. The mod_rewrite engine helps achieve the redirect goals through .htaccess in this case. Lately, many users don’t add www when they are entering URLs in the search bar.

The code to achieve this looks like this:

RewriteEngine On

RewriteCond % ^forinstance.com [NC

RewriteRule ^(.*) http://www.website.com/$1 [R=301,NC]

Redirecting www to non-www using .htaccess

As we just mentioned, users are steering away from www subdomain in recent years. But many of them are still bound by habit formed over years to just type in www before every domain name. In case your website doesn’t use www subdomain, you should be able to bring these users to your website by redirecting the URL they enter. The mod_rewrite module gets the job done. The example code looks like this:

Options +FollowSymlinks

RewriteEngine on

RewriteCond % ^www.forinstance.com [NC]

RewriteRule ^(.*)$ http://forinstance.org/$1 [R=301,NC]

Writing SEO friendly URLs

Technical SEO can get affected by the URL structure for your website and its different pages. A well-written URL structure can be easily read and comprehended by both the users and the search bots. If written carefully and after putting in some thought, a URL structure can help the search boat understand the content within a web page.

Once your website gains authority in the search engine results for your niche, you should make it a point to pay attention to your URL structure. You can use the .htaccess file to restructure your URLs and boost your technical SEO.

You also get the functionality to remove .php and .html by adding directives to .htaccess.
Here are some examples of the stuff you can do using .htaccess to the structure of URLs:

  1. 1. How to use .htaccess to turn the URLs to lowercase?

    You can use one of the two following methods:

    • a. RewriteCond %{REQUEST_URI} [A-Z]
      RewriteRule ^.+.html$ ${lc:%{REQUEST_URI}} [NC,R=301,L]

      Here, only the html file names will get affected.

    • b. RewriteCond %{REQUEST_URI} [A-Z]
      RewriteRule . ${lc:%{REQUEST_URI}} [R=301,L]

      Here, the code will impact every URL structure.

  2. 2. How to use .htaccess to remove extensions in URLs?
    • a. When removing .html

      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME}.html -f
      RewriteRule ^(.*)$ $1.html [NC,L]

    • b. When removing .php

      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME}.php -f
      RewriteRule ^(.*)$ $1.php [NC,L]

      You can do so much using .htaccess to change the structure of URLs.

Implementing Hot link Protection

Hot linking occurs when some other website sources assets from your website, like video, images, documents, etc. Your website’s bandwidth is overloaded unnecessarily. You may end up paying more for hosting without actually getting a tangible benefit out of the hot links.

You can disallow other domains from showing your content on their sites using .htaccess. You can look at the sample code below to see how it works:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mywebsite.com/.*$ [NC]
RewriteRule \.(mp4|jpg|png)$ – [F]

Here, we have used .htaccess to prevent .mp4, .jpg, png files from being linked to any domain other than https://mywebsite.com. The idea is that you can use .htaccess to disallow any file format from being displayed on other websites.

You can also display a message in place of the file on the external site that tries to display your content. For example, if they are trying to share a png file on their website from yours, you can use .htaccess to display a message in its place that could say something like:

“The file that you are trying to view is from mywebsite.com.”

The example code will look something like this:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://(www\.)?mycoolsite.com/.*$ [NC]
RewriteRule \.(png|jpg|mp4)$ https://www.mywebsite.com/hotlink-error-message.jpg [R,L]

Conclusion

By now, we have seen how the .htaccess file can do wonders for your website in terms of its technical SEO. You can use the .htaccess file to improve the speed of your website, increase its security, prevent sharing of content, redirect old URLs, and so much more.

The .htaccess file is a boon for technical SEO. But at the same time, you should be careful while using .htaccess because even a small error in the syntax can create massive errors in the directories. You can bring your website down if you don’t know how to use the .htaccess file properly. Therefore, we would advise you to tread with caution.

The possibilities of positively impacting SEO using the .htaccess file are endless.