.htaccess is used very common nowadays. To create the .htaccess file you can open notepad and save the file with name of .htaccess.

htaccess tricks

htaccess tricks

So here are some popular htaccess tricks.

If you want that if you type example.com in your url and it is changed to http://www.example .com then you can do this by writing the following code in .htaccess file:-


RewriteCond %{HTTP_HOST} ^example\.com$ [NC]

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

This is good for seo point of view because example.com and http://www.example.com are two different url for search engines.

If you want to show all the files in the directory if there is also index.html or index.php exists in the directory then you write the code as follows:-


Options +Indexes

IndexOptions FancyIndexing

Suppose there is no index.php file in your directory and there is a file home.php and if want that home.php will open as default page then you can do this as:-


DirectoryIndex home.php

For hiding files like you want to hide the files with extension of .txt and .ico then write the code as:-


IndexIgnore *.txt *.ico

If you have a file with your own extension of .jon and you want that file to open as php file then do write this code in .htaccess file


AddType application/x-httpd-php .jon

If you want to restrict some file from viewing then you can also do this in .htaccess file. Suppose you want to restrict myfile.txt then here the code:-

<files myfile.txt>
  order allow,deny
  deny from all
</files>

Dont place space between allow and deny

If you want to restrict all the file of extension .txt and .ico then write this code:-

<FilesMatch "\.(txt|ico)$">
  order allow,deny
  deny from all
</FilesMatch>

If you want add default char set then here u can do this:-


AddDefaultCharset utf-8


Share This Story, Choose Your Platform!