Most Useful Apache ".htaccess" tricks to customize and secure your website.

1. How to allow or deny Access to website.

To Allow:
Order Allow, Deny
Deny from All
Allow from 192.168.1.10

or 

Order Allow, Deny
Allow from 192.168.1.10



Note: Order means that process the command according to their position, i.e. Allow command process first and Deny command process after completing the Allow command.

To Deny:
Order Allow, Deny
Deny from 192.168.1.10
Allow from All

or 

Order Deny, Allow
Deny from 192.168.1.10


2. For different error codes, generating different error documents in Apache.

ErrorDocument 404 /errdoc/error-404.htm

Where 404 is error code and "/errdoc/error-404.htm" is the page which displays when an 404 error occurred.

3. Defining MIME types for files.
<IfModule mod_mime.c>
AddType application/vbscript vb
AddType application/x-font-ttf ttf ttc
</IfModule>

Where application/vbscript is MIME TYPE and vb is its extension.

4. Make available mp3 files or video files for download.

AddType application/octet-stream .mp3 .zip 

Where application/octet-stream is the KEY and ".mp3 .zip" is value for that key. Multiple value can be added for the same key.


5. How to stop executing some files and display as source code in the web browser.


RemoveHandler cgi-script .php .pl .py
AddType text/plain .php .pl .py 


Where .php, .pl, .py is executable files which runs on the server and its output displays on the client.


6. How to limit the uploading and downloading size.


php_value upload_max_filesize 50M
php_value post_max_size 50M
php_value max_execution_time 500
php_value max_input_time 500



7. How to setup directory index for website.

DirectoryIndex index.html index.php mypage.php index.myextension



8. How to setup time zone for website.

SetEnv TZ India/Delhi


Where TZ is the global variable for TIME ZONE and "India/Delhi" is the zone(value) for TZ Key.


9. How to setup GZip compression for files on the website.

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/html
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

Where text/plain text/html etc... are the MIME types which will compress and send to client when requested.

10. How to enable cache control for website.


<FilesMatch "\.(ico|png|jpeg|svg|ttf)$">
Header Set Cache-Control "max-age=3600, public" </FilesMatch> <FilesMatch "\.(js|css)$"> Header Set Cache-Control "public" Header Set Expires "Sat, 24 Jan 2015 16:00:00 GMT" </FilesMatch>

Where 3600  is time limit in second or "Tue, 27 Jan 2015 10:54:00 GMT",, after this time cache remove from website and new file download if website again open.

11. How to redirect entire website to a secure connection(https).

RewriteEngine ON
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

12. How to run CGI script outside of "cgi-bin" folder.

AddHandler cgi-script .cgi
Options +ExecCGI


13. How to rename the HTACCESS File.

AccessFileName secret.cess



14. How to configure individual file in HTACCESS.


<files conf.html="">
Order allow, deny
Deny from 188.100.100.0
</files>



Where secret.html is the file name to conigure.


15.How to prevent website directory listing.

Options -Indexes


16. How to Prevent Image Hotlinking for a Website

RewriteEngine ON
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERERER} !^http://(www\.)?mywebsite.com/.*$ [NC]
RewriteRule \.(gif|jpeg|png)$ - [F].



17. How to Redirect users to a Maintenance page.

RewriteCond %{REQUEST_URI} !^/admin/ [NC]
RewriteCond %{REQUEST_URI} !^((.*).css|(.*).js|(.*).png|(.*).jpg) [NC]
RewriteRule ^(.*)$ /errpage/maintainence.html
[NC,L,U,QSA]



18. How to Redirect non-www to www.

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

19. How to map a domain to IP Address.

For Mapping L.M.N.O address to a domain name www.google.com
RewriteCond %{HTTP_HOST} ^L\.M\.N\.O$ [NC]
RewriteRule ^(.*)$ http://www.google.com/$1 [L,R=301]

20. How to change Default charset and language headers.

AddDefaultCharset UTF-8
DefaultLanguage en-US

No comments:

PageNavi Results No.

Welcome. Here you find latest freeware and legal software as well as latest info about IT Technology.

Recent Posts

recentposts
Powered by Blogger.