Home  ❭  Blog  ❭  16 Great Htaccess Tricks And Hacks
  • 16 great .htaccess Tricks and Hacks
  • The Apache web server has quite a few configuration options that are accessible to the server administrator. The .htaccess is often a simple ASCII text file used in your website root directory. In like manner avoid that always seek to keep a backup copies off your files through the server before using the services of the. htaccess document.

     

    1. Creating a custom error page with .htaccess

     

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

     

    2. How to set the timezone on your server

     

    SetEnv TZ America/Houston

     

    3. Block IPs Using htaccess

     

    allow from all
    deny from 123.176.11.147
    deny from 123.45

     

    4. SEO Friendly 301 permanent redirects

     

    Redirect 301 /d/file.html http://www.htaccesselite.com/r/file.html

     

    5. Set the Email Address for the Server Administrator

     

    ServerSignature EMail
    SetEnv SERVER_ADMIN default@domain.com

     

    6. Hotlinking protection

     

    Options +FollowSymlinks
    # Protect Hotlinking
    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www.)?domainname.com/ [nc]
    RewriteRule .*.(gif|jpg|png)$ http://domainname.com/img/hotlink_f_o.png [nc]

     

    7. Block all requests from user agent

     

    ## .htaccess Code :: BEGIN
    ## Block Bad Bots by user-Agent
    SetEnvIfNoCase user-Agent ^FrontPage [NC,OR]
    SetEnvIfNoCase user-Agent ^Java.* [NC,OR]
    SetEnvIfNoCase user-Agent ^Microsoft.URL [NC,OR]
    SetEnvIfNoCase user-Agent ^MSFrontPage [NC,OR]
    SetEnvIfNoCase user-Agent ^Offline.Explorer [NC,OR]
    SetEnvIfNoCase user-Agent ^[Ww]eb[Bb]andit [NC,OR]
    SetEnvIfNoCase user-Agent ^Zeus [NC]

    Order Allow,Deny
    Allow from all
    Deny from env=bad_bot

    ## .htaccess Code :: END

     

    8. Redirect everyone to different site except few IP

    ErrorDocument 403 http://www.youdomain.com
    Order deny,allow
    Deny from all
    Allow from 124.34.48.165
    Allow from 102.54.68.123

     

    9. Don’t want to display download request

     

    AddType application/octet-stream .pdf

    AddType application/octet-stream .zip

    AddType application/octet-stream .mov

     

     

    Posted by – Gaurav Kumar (Php Developer)

    Shyamsoft Technologies