How to Create .htaccess File?

The .htaccess file contains instructions that request the server how to behave in different scenarios and how run your website functions. It’s very important file to control or boost your website performance even you can make your pages more secure and useable with this file. you can hide file extension through .htacces to get better url structures.  Here are the list of some features that you can enable or disable using a .htaccess like file caching, URL redirection, password protection, remove file extension and set custom error pages.

Creating the htacces file is very easy, here I am sharing the details step by step.
First you need to open a blank text file like notepad (win+r)

how to create .htaccess file

Now save the file exactly like this: “.htaccess”
Don’t forget to include first and last “(coma) refer below image to understand

If you have saved the file then you can do anything using queries and directives

Example
This example will redirect http://example.com/folder1/ to http://example.com/folder2/.
In place of example.com you can write you domain name and you can change folder name as well.

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1.*$ http://example.com/folder2/ [R=301,L]


Another work of .htaccess file, How to set leverage browser caching
We can use .htaccess file to control browser cache, here is the code which can be used. The code below tells browsers what to cache and how long to “remember” it.

ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"


How to Remove File Extension (.php, .html) from URL using .htaccess
As per SEO experts, there is no issue if your website URLs having .php, .htm or .html extension in there URL but I’m sharing some advantage which can make your website beautiful and user friendly.

  • Back-end technology is hidden from users end but you can identifiy easy name and extension. of file.
  • If you remove file extension you will get clean and notable path.
  • We can easily change backend technology without affecting SEO of the pages.
  • If you redesign your website, your shared links will be same.

Removing .php Extension from URL
For example, you need to convert your URL from http://example.com/mypage.php to http://example.com/mypage. new .htaccess file and add following settings.

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

Removing .html Extension from URL
For example you need to convert your url from http://example.com/mypage.html to http://example.com/mypage. new.htaccess file and add following settings.

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

Leave a Reply

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