• Giao hàng miễn phí toàn quốc - TP HCM giao tận nơi
  • Smallrig phụ kiện hỗ trợ quay điện thoại Iphone
  • Smallrig phụ kiện cage, l plate, rig, tools
  • Tilta Phụ kiện chính hãng
  • Phụ kiện Gopro 11 10 9 8 7 6 5

17 Useful Htaccess Tricks and Tips

  Thủ thuật tối ưu Htaccess. Giúp bạn tùy chỉnh Htaccess theo ý.


The following htaccess will able to help you to achieve simple task such as redirection and web server optimization.

1. Set Timezone

Sometimes, when you using date or mktime function in php, it will show you a funny message regarding timezone. This is one of the way to solve it. Set timezone for your server. A list of supported timezone can be found here

  1. SetEnv TZ Australia/Melbourne

2. SEO Friendly 301 Permanent Redirects

Why it's SEO friendly? Nowadays, some modern serach engine has the capability to detect 301 Permanent Redirects and update its existing record.

  1. Redirect 301 http://www.queness.com/home http://www.queness.com/

3. Skip the download dialogue

Usually when you try to download something from a web server you get a request asking whether you want to save the file or open it. To avoid that you can use the below code on your .htaccess file

  1. AddType application/octet-stream .pdf
  2. AddType application/octet-stream .zip
  3. AddType application/octet-stream .mov

4. Skip www

One of the SEO guideline is, make sure there is only one URL pointing to your website. Therefore, you will need this to redirect all www traffic to non-ww, or the other way around.

  1. RewriteEngine On
  2. RewriteBase /
  3. RewriteCond %{HTTP_HOST} ^www.queness.com [NC]
  4. RewriteRule ^(.*)$ http://queness.com/$1 [L,R=301]

5. Custom Error page

Create a custom error page for each of the error codes.

  1. ErrorDocument 401 /error/401.php
  2. ErrorDocument 403 /error/403.php
  3. ErrorDocument 404 /error/404.php
  4. ErrorDocument 500 /error/500.php

6. Compress files

Optimize your website loading time by compressing files into smaller size.

  1. # compress text, html, javascript, css, xml:
  2. AddOutputFilterByType DEFLATE text/plain
  3. AddOutputFilterByType DEFLATE text/html
  4. AddOutputFilterByType DEFLATE text/xml
  5. AddOutputFilterByType DEFLATE text/css
  6. AddOutputFilterByType DEFLATE application/xml
  7. AddOutputFilterByType DEFLATE application/xhtml+xml
  8. AddOutputFilterByType DEFLATE application/rss+xml
  9. AddOutputFilterByType DEFLATE application/javascript
  10. AddOutputFilterByType DEFLATE application/x-javascript

7. Cache files

File caching is another famous approach in optimizing website loading time

  1. <FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
  2. Header set Cache-Control "max-age=2592000"
  3. FilesMatch>

8. Disable caching for certain file type

Well, in the other hand, you can disable caching for certain file type.

  1. # explicitly disable caching for scripts and other dynamic files
  2. <FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$">
  3. Header unset Cache-Control
  4. FilesMatch>


The following htaccess code will able to enhance the security level of your webserver. Hotlinking protection is pretty useful to avoid other people using images that stored in your server.

1. Hotlinking protection with .htaccess

Hate it when people stealing bandwidth from your website by using images that are hosted in your web server? Use this, you will able to prevent it from happening.

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

2. Prevent hacks

If you want to increase the security level of your website, you can chuck these few lines of codes to prevent some common hacking techniques by detecting malicious URL patterns.

  1. RewriteEngine On
  2. # proc/self/environ? no way!
  3. RewriteCond %{QUERY_STRING} proc/self/environ [OR]
  4. # Block out any script trying to set a mosConfig value through the URL
  5. RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
  6. # Block out any script trying to base64_encode crap to send via URL
  7. RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
  8. # Block out any script that includes a <script> tag in URL
  9. RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
  10. # Block out any script trying to set a PHP GLOBALS variable via URL
  11. RewriteCond %{QUERY_STRING} GLOBALS(=|[|\%[0-9A-Z]{0,2}) [OR]
  12. # Block out any script trying to modify a _REQUEST variable via URL
  13. RewriteCond %{QUERY_STRING} _REQUEST(=|[|\%[0-9A-Z]{0,2})
  14. # Send all blocked request to homepage with 403 Forbidden error!
  15. RewriteRule ^(.*)$ index.php [F,L]

3. Block access to your .htaccess file

The following code will prevent user to access your .htaccess file. Also, you can block multiple file type as well.

  1. # secure htaccess file
  2. <Files .htaccess>
  3. order allow,deny
  4. deny from all
  5. Files>
  6. # prevent viewing of a specific file
  7. <Files secretfile.jpg>
  8. order allow,deny
  9. deny from all
  10. Files>
  11. # multiple file types
  12. <FilesMatch ".(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
  13. Order Allow,Deny
  14. Deny from all
  15. FilesMatch>

4. Rename htaccess files

You can also rename your .htaccess file name to something else to prevent access.

  1. AccessFileName htacc.ess

5. Disable directory browsing

Avoid the server from displaying directory index, or the opposite.

  1. # disable directory browsing
  2. Options All -Indexes
  3. # enable directory browsing
  4. Options All +Indexes

6. Change default Index page

You can change the default page index.html, index.php or index.htm to something else.

  1. DirectoryIndex business.html

7. Block unwanted visitor based on referring domain


  1. # block visitors referred from indicated domains
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine on
  4. RewriteCond %{HTTP_REFERER} scumbag.com [NC,OR]
  5. RewriteCond %{HTTP_REFERER} wormhole.com [NC,OR]
  6. RewriteRule .* - [F]
  7. ifModule>

8. Blocking request based on User-Agent Header

This method could save your bandwidth quota by blocking certain bots or spiders from crawling your website.

  1. # block visitors referred from indicated domains
  2. <IfModule mod_rewrite.c>
  3. SetEnvIfNoCase ^User-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT
  4. SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT
  5. Deny from env=HTTP_SAFE_BADBOT
  6. ifModule>

9. Secure directories by disabling execution of scripts


  1. # secure directory by disabling script execution
  2. AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
  3. Options -ExecCGI
Share facebookShare facebook

Tin Cùng Chuyên Mục

Trang 1 / 1

Bạn đã đọc tin này chưa ?

Go Top
Chat hỗ trợ
Chat ngay