How to get the most out of your .htaccess file?

Updated on January 25, 2022

The .htaccess file is a crucial website file that manages a website’s configuration. The .htaccess is a configuration file in Apache-based web servers that adds more configuration options such as redirection, security, PHP settings, etc. It enables you to make changes to your website setup without having to edit server configuration files.

In the knowledge base article, we will share the primary and most used .htaccess rules which are ONLY applicable on LAMP Stack running on Apache

Table of Content:

  1. Forcing HTTP to HTTPS
  2. Forcing SSL and www
  3. Forcing SSL and non-www
  4. Redirect from non-www to www
  5. Redirect from www to non-www
  6. Redirect a domain to sub-directory
  7. Redirect old domain to new domain
  8. Block wp-login.php
  9. Block access to specific IP(s)
  10. Block IP with exception(s)
  11. 301 Permanent Redirect
  12. 302 Temporary Redirect
  13. Most common error codes

Let’s begin the steps for LAMP Stack. 

First, you must connect your server via SSH or SFTP using Master User or Application User.

Note: You can see the instructions on how to connect your application via SSH and SFTP from our knowledge base article.

1) Forcing HTTP to HTTPS

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

2) Forcing SSL and www

RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301,NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

3) Forcing SSL and non-www

RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

4) Redirect from non-www to www

RewriteEngine On
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

5) Redirect from www to non-www

RewriteEngine On
RewriteRule ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^$1 [L,R=301]

6) Redirect a domain to a sub-directory

RewriteCond %{HTTP_HOST} ^$
RewriteCond %{REQUEST_URI} !^/sub-directory name/
RewriteRule (.*) /subdir/$1

7) Redirect Old Domain to New Domain

RewriteEngine on
RewriteCond %{HTTP_HOST} !$ [NC]
RewriteRule^(.*)$$1 [L,R=301,NC]

8) Block wp-login.php

<Files wp-login.php>
order deny,allow
deny from all
allow from x.x.x.x

9) Block access to specific IP(s)

# Block Single/Multiple IP
<Limit GET POST>
order allow,deny
deny from x.x.x.x
deny from xx.xx.xx.xx
allow from all

10) Block IP with exception(s)

# Just Allow a Single IP
<Limit GET POST>
order deny,allow
deny from all
allow from xx.xx.xx.xx

11) 301 Permanent Redirect

Redirect 301 /

12) 302 Temporary Redirect

Redirect 302 / to

13) Most common error codes

Creating custom error pages is essential since it allows you to offer website visitors a nice error message, such as if a URL on your website does not work. Simply construct your own custom error page and save it in the webroot or another directory. This avoids the inconvenient 404 File Not Found error.

Here, we will take an example of a 404 error and a document called 404.html placed in a different directory called error.

The document 404.html, like the others on your website, is a standard HTML document that can display whatever content you add in the custom error document.

a) Client-Side Error

  1. 400 – Bad request
  2. 401 – Authorization Required
  3. 403 – Forbidden
  4. 404 – File Not Found

b) Server-Side Error

  1. 500 – Internal Server Error
  2. 501 – Not Implemented
  3. 502 – Bad Gateway
  4. 503 – Service Unavailable

a) Client-Side Error

ErrorDocument 400 /error/custom400.html
ErrorDocument 401 /error/custom401.html
ErrorDocument 403 /error/custom403.html
ErrorDocument 404 /error/custom404.html

b) Server-Side Error

ErrorDocument 500 /error/custom500.html
ErrorDocument 501 /error/custom501.html
ErrorDocument 502 /error/custom502.html
ErrorDocument 503 /error/custom503.html
ErrorDocument 503 /error/custom504.html

Note: If you encounter a 5XX Server Error, your custom error page may be unavailable. It is determined by the precise nature of the error. Nonetheless, it would be best if you create them.

Need More Help!

Type in your question at the knowledge base website or comment below

Leave a Comment

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