The enormous possibilities of the .htaccess file

The enormous possibilities of the .htaccess file

GET IN TOUCH WITH US

Recently we had to slog over the .htaccess file for a new website we were launching. Considering how much we had to search for the right information, we thought of putting together our learnings here.

If you are a webmaster or in charge of marketing a website, you need to have a basic understanding of the .htaccess file - how to create it, how to edit it, and the enormous possibilities of code that can be written in the simple .htaccess file.

What is an .htaccess file?

.htaccess is a simple file added to the root of your website, that instructs the server about redirects and a few crucial things. We will look at them in more detail in this article.

How to create a .htaccess file?

Create a new file on Notepad, and save it as ‘.htaccess’. Remember that it should NOT have a .txt extension. This file should be uploaded to the root directory of your website.

You can edit this file using any HTML editor, or even Notepad, and upload it using an FTP client like Filezilla or Cyberduck.

How to redirect pages using .htaccess?

There are a bunch of redirects you can do using the .htaccess file, from an old page to a new page, from an old website to a new website and so on.

Before adding any redirects to your .htaccess file, add this code to locate the Rewrite Engine that’s usually in the mod-rewrite module in Apache.

<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>

  1. 301 and 302 redirects

    We’ve all heard these terms and know that 301 means permanent redirect and 302 means temporary redirect. But how do we write these in the .htaccess file, and what difference does it make to the search engines?

    Each redirect is a new line written with the prefix 'RedirectMatch', which changes depending on whether you are doing a 301 or 302 redirect.

    RedirectMatch 301
    RedirectMatch 302

    301 or Permanent Redirect instructs search engines to ignore the old page in the index, and move toward the new page. Search engines might or might not respect this, sometimes indexing both URLs, and sometimes continuing to index the old URL.

    But eventually, all search engines move the value of your old URL to the new URL once you do a 301 redirect.

    Hence it’s recommended to do a 301 redirect if you are in doubt.

    302 or temporary redirect will not affect your SEO rankings with search engines. It’s considered a temporary measure while you are testing out a new design, navigation, or offer.

  2. Redirecting from one page to another

    You usually need to redirect a page to another when:

    • The old page does not exist (moving from CMS to PHP website, or vice versa)
    • The page content is not relevant anymore (like contests, offers, trends for a certain year, etc.)
    • You don’t offer that service or product anymore

    In these cases, you can redirect the old page to the new page using this code.

    RedirectMatch 301 /oldpage/ /newpage/

    This can be written with or without the full URL. Both the options below work the same way.

    RedirectMatch 301 /resources/digital-marketing-trends-2021/ /resources/digital-marketing-trends-2022/

    RedirectMatch 301 https://www.tagbeansdigital.com/resources/digital-marketing-trends-2021.php https://www.tagbeansdigital.com /resources/digital-marketing-trends-2022.php

  3. Redirecting a page to another website

    Though this is a rare occurrence, it is possible that you need to redirect a certain page on your website to another website.

    RedirectMatch 301 https://www.tagbeansdigital.com/resources/digital-marketing-trends-2022/ https://www.digitalmarketingtrends.com

    The key point to remember here is that you can redirect a page on your website to any page, folder, or website you want to, using the .htaccess file.

  4. Redirecting from one folder to another

    You might need to redirect from one folder to another when:

    • You want to rename a folder or directory
    • You want to delete a directory and move all files to the root directory

    The code below redirects all pages under the /articles folder to the home page of the website.

    RewriteRule ^articles/(.*)$ / [R=301,L]

    And this code redirects all pages under /articles folder to a new folder called /digital-marketing-resources/

    RewriteRule ^articles/(.*)$ /digital-marketing-resources/ [R=301,L]

  5. Redirecting non-www version of a website to www version

    Most webmasters prefer the www version of their website to be indexed in search engines. And it’s always good to follow the same version everywhere, be it Google Analytics, Google Search Console, Sitemap.xml etc.

    Any visitor who comes to the non-www version should be redirected to the www version of your websit to ensure better search engine indexing and site performance.

    Just add these 2 lines of code to your .htaccess file to redirect from non-www version of your website to www version.

    RewriteCond %{HTTP_HOST} ^tagbeansdigital.com
    RewriteRule (.*) https://www.tagbeansdigital.com/$1 [R=301,L]

  6. Redirecting from HTTP to HTTPS version

    SSL certificates are essential for all websites to ensure privacy of data, and to convey trust to your visitors. Once the SSL certificate is added to your website, adding these lines to your .htaccess file can redirect all the HTTP traffic automatically to the HTTPS version.

    RewriteCond %{SERVER_PORT} ^80$
    RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

  7. Adding Custom 404 page

    You do not want your customers to land on a 404 page on your website, ever. But there can be mistakes in URLs typed by customers, or mistakes in incoming links from third-party websites, resulting in your customers landing on 404 pages.

    The best solution to this is to create a custom 404 page for your website, that looks just like your normal website page. You can give links to prominent pages from this Custom 404 page so that your visitors can navigate easily to relevant pages.

    Instruct browsers to load these Custom 404 pages using a simple code in the .htaccess file.

    ErrorDocument 404 /404.php

    Voila! Customers who type random URLs like https://www.tagbeansdigital.com/dfadcc will land on your Custom 404 page.

  8. Regular Expressions

    There are many regular expressions you can use to redirect URLs that follow certain patterns. You can find a comprehensive list of regular expressions here.

    Below is a simple Regular Expression you can use to redirect all your old blog pages to the new page for Resources.

    RedirectMatch 301 https://www.tagbeansdigital.com/blog/*.php https://www.tagbeansdigital.com/resources/

Beware While Using .htaccess File

Now that we know the enormous possibilities of the .htaccess file, we should also understand the limitations, or things to be aware of while writing code in the .htaccess file.

  • Size of the .htaccess file – It is very important to keep your .htaccess file small in terms of size and lines and code. Adding too many redirects can slow down your website. Remember that your server will read the .htaccess file to check for redirects before rendering each request.
  • Too many hops in your redirects - When Page A redirects to Page B and then redirects to Page C, it can cause confusion resulting in poor website performance in terms of SEO and page loading time.
  • Wrong regular expressions – Regular Expressions are very potent, and can redirect entire series of URLs, if not written correctly.

So be careful to write only the bare minimum redirects in the .htaccess file. And to test the redirects thoroughly once you publish them.

Your hosting provider would have Configurations options to specify redirects outside of the .htaccess file, and code written at this level will have precedence over the .htaccess file. So, you could write more important or permanent redirects at the Config level.

If you need help with configuring your .htaccess file correctly, reach out to Tagbeans Digital today. We can help sculpt your digital marketing success story.

Get in touch with us

 
Address
Sobha Petunia, Nagavara, Bangalore. India