Today's Question:  What does your personal desk look like?        GIVE A SHOUT

What can .htaccess file do?

  Peter        2012-06-15 06:39:39       42,904        0    

A .htaccess file is a directory-level configuration file supported by several web servers, that allows for decentralized management of web server configuration. What can a .htaccess file do? We summarized some of them here.  Hope it may help you.

1. Timezone setting


Sometimes when you use date() or mktime() functions in PHP, , it may display some weird information because of the timezone difference. For example, one possible warning is :

Warning
: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function.

We can use the date_default_timezone_set() function to set our default timezone. Another solution is to set the timezone of the web server in the .htaccess file.  Add the below line to the .htcaccess file.

SetEnv TZ Australia/Melbourne

You can find all the supported timezones for PHP here.

2. Search engine friendly 301 moved permanently redirection

If we redesign our website, some of the URLs and paths on the site may change. To prevent getting 404 File Not Found error, we can use .htaccess 301 redirect. .htaccess redirect is better than the meta refresh or redirect tag because there is no delay as the browser reads the .htaccess file first. We can add one line similar to the following one to the .htaccess file to realize redirect:

Redirect 301 http://www.example.com/home http://www.example.com

This means redirect the http://www.example.com/home to http://www.example.com. You can add more lines if you have more page redirection. 

3. Block the download prompt dialog

Usually, when you need to download something from the Internet, you may see a dialog popped out to ask you whether you want to save the file or open the file directly. If you don;t want to see this dialog, you can add these codes to the .htaccess file:

AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .mov
...

4. Remove the www prefix

One rule of SEO is to ensure that your website has only one URL. Hence you may need to direct all requests from www prefixed URL www.example.com to you hostname: example.com, or vice versa.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [LR=301]

For some web servers, in order to allow rewrite url, some configuration files need to be modified,  too, for example httpd.conf for Apache

5. Customize Error page

Use customized error page for different request error.

ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php

6. File compression

By compressing the source code generated by the web server before they are sent to the client,  it can reduce the transmission time.

AddOutputFilterByType DEFLATE text/plain 
AddOutputFilterByType DEFLATE text/html 
AddOutputFilterByType DEFLATE text/xml 
AddOutputFilterByType DEFLATE text/css 
AddOutputFilterByType DEFLATE application/xml 
AddOutputFilterByType DEFLATE application/xhtml+xml 
AddOutputFilterByType DEFLATE application/rss+xml 
AddOutputFilterByType DEFLATE application/javascript 
AddOutputFilterByType DEFLATE application/x-javascript 

7. File cache

File cache is another way to improve your website access speed

<FilesMatch “.(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$”> 
Header set Cache-Control “max-age=2592000″ 
</FilesMatch> 

8. Disable file cache for some specified  files

You can explicitly disable file cache some particular type of files

<FilesMatch “.(pl|php|cgi|spl|scgi|fcgi)$”> 
Header unset Cache-Control 
</FilesMatch> 

9. Prevent link stealing

Do you hate those who link to you image resources on your web server to take up your bandwidth? Try this:

RewriteBase / 
RewriteCond %{HTTP_REFERER} !^$ 
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC] 
RewriteRule .(gif|jpg|swf|flv|png)$ /feed/ [R=302,L] 

10. Disable directory listing

Sometimes if you don't have an index page in a folder and other people typed a URL(http://www.example.com/home) which is to get the index page in that folder, instead of displaying the index page, the files in that directory will be listed since it cannot find the index page. This is unsafe for the website since others can see your website structure. You can disable directory listing with:

Options All -Indexes

If you want to allow directory listing, you can use:

Options All +Indexes

11. Change the default index page

You can change the default index.html, index.php or index.htm to other pages

DirectoryIndex example.html 

12. Disable script execution in some directories

#Disable script execution right in some directories
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi 
Options -ExecCGI

There are many others thing .htaccess can do. You can comment below to let others know more about .htaccess file.

Reference : http://www.aqee.net/htaccess-usage/

TIP  .HTACCESS  SETTING 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.