What is .htaccess file and how to use it

AuthorHariom Prajapati

Pubish Date26 Jun 2022

categoryArticle

When you develop the website, a file appears, which we know by the name of .htaccess. Using this we can configure the server. 

In this tutorial, we will see what is .htaccess file and how to use it.

What is .htaccess file

The .htaccess file is a configuration file of Apache web server software with the help of which you can enable or disable the additional functionality and features of Apache web server software.

How to use .htaccess file

The .htaccess file is a configuration file of the Apache Web Server software that allows you to enable or disable additional functionality and features of the Apache Web Server software.

How to remove .HTML extension from URL using .htaccess file

First of all i clear you that if you don't remove .HTML extension then it does not impact on your website. Actually, by removing .HTML from the URL, we can make the URL lenght smaller and beautiful to look.

For example :-

www.teknowize.com/about.html 

To

www.teknowize.com/about

 

Paste the below code to your .htaccess file-

Code

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.+)$ $1.html [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/ RewriteRule ^(.*)\.html$ /$1 [R=301,L]

 

How to remove .PHP extension from URL using .htaccess file

First of all I clear you that if you don't remove .PHP extension then it does not impact on your website. Actually, by removing .PHP from the URL, you can make the URL lenght smaller and beautiful to look.

For example :-

www.teknowize.com/about.php

To

www.teknowize.com/about

 

Paste the below code to your .htaccess file.

Code

RewriteEngine On 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] 
RewriteRule ^ %1 [R=301,L] 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*?)/?$ $1.php [NC,L]

 

How to prevent index of page using .htaccess file

It is most important for security purpose because if anyone write your "domain name/folder name" then they can access your all the file present in that folder, so we need to prevent this for security purpose by using 403 forbidden.

 

Paste the below code in your .htaccess file -

Code

Options -Indexes

Custom 403 page using .htaccess file

If you want to add custom 403 Forbidden page then you need to create 403 Forbidden page firstly and then paste the below code to your .htaccess file -

Code

errorDocument 403 /assets/403_page.html

 

Redirect non-www to www using using .htaccess file

If your website have non-www or www then don't worry because www or non-www are basically useless and it never impact on your website but if you want to transfer non-www to domain to www domain then paste below code to your .htaccess file.

Code

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

 

Redirect www to non-www using .htaccess file

If you want to transfer www to non-www then please keep in mind that this is useless because if your website have www or non-www then the value of both are same.so no need to transfer but if you want then paste the below code to your .htaccess file -

Code

RewriteEngine On 
RewriteCond %{HTTP_HOST} www.example.com 
RewriteRule (.*) http://example.com/$1 [R=301,L]

 

Redirect http to https using .htaccess file

It is most important for your website. If your website don't have SSL certificate then buy SSL certificate or you can get it free on Internet. It is important because if your website have non-https then the user get a message which is "not secure" so please activate SSL certificate on your website for security purpose and then paste the below code to your access file according to your need.

 

Redirect Only a Specific Domain 

For redirecting http to https to all folder or web pages present in your server 

Code

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.com [NC] 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

 

Redirect Only a Specific Folder

For redirecting http to https to specific folder present in your server 

Code

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} folder 
RewriteRule ^(.*)$ https://www.example.com/folder/$1 [R,L]

Note: Please replace “example” with your domain name and also replace /folder with your folder name.

Comments 0

Leave a comment