Popular htaccess tricks


.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:-

[code lang=”php”]

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

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

[/code]

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:-

[code lang=”php”]

Options +Indexes

IndexOptions FancyIndexing

[/code]

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:-

[code lang=”php”]

DirectoryIndex home.php

[/code]

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

[code lang=”php”]

IndexIgnore *.txt *.ico

[/code]

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

[code lang=”php”]

AddType application/x-httpd-php .jon

[/code]

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:-

[code lang=”php”]
<files myfile.txt>
order allow,deny
deny from all
</files>
[/code]

Dont place space between allow and deny

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

[code lang=”php”]
<FilesMatch "\.(txt|ico)$">
order allow,deny
deny from all
</FilesMatch>
[/code]

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

[code lang=”php”]

AddDefaultCharset utf-8

[/code]


Leave a Comment

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