Tweaks & Tricks with .htaccess

Discussion in 'Web Development' started by pradeep, Oct 25, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in

    Introduction



    A really cool feature that web designers overlook is the use of the .htaccess file. Simply put, whenever you see a custom error page (404, 403, etc.), use server side includes, restrict IPs, load a default page, or do any host of events invisible to the user, you are probably going to be using .htaccess.

    I will provide further examples of items you can add to an .htaccess file beyond just custom error pages.

    .htaccess is a simple text file that you add and edit in a text editor such as Notepad. You save the file as .htaccess without any extra characters before or after the name of the file. I know the file's name is a little strange. Then again, most cool things are.

    Anyway, the following paragraphs will try to highlight some of the more useful .htaccess features and the code required to get those features to work. To test the following features, copy and paste the line(s) of code that you want into Notepad, save the file as .htaccess, and upload the file to the root web directory of your web server.

    Here goes:

    Custom Error Pages



    Don't you hate it when people access your site via a bad link and are faced with an error message such as "Permission Denied" or "Page Cannot be Found"? Well, you can liven up the error messages by creating custom error pages for each type of error you anticipate your visitors will encounter:

    ErrorDocument 404 http://www.go4expert.com/404.htm
    ErrorDocument 401 /admin/authorization.htm
    ErrorDocument 403 /password/forbidden.htm

    To create custom error pages, replace the error code next to ErrorDocument to the appropriate error code, and change the URL of the page that will receiving the error-prone user! For example, here is this site's 404 error page: http://www.go4expert.com/404.htm

    Redirects



    Here is a nightmare scenario. You decide to shift an entire folder's worth of pages and images into a new folder on your server. If you aren't using any 3rd party programs to update any links to the new location, your users are going to be seeing a lot of the Custom error pages you may have used previously.

    A solution would be to the .htaccess file's redirect feature. For example, if you are interested in redirecting users from a folder called temp to another folder, you would use the following code:

    Redirect /temp http://www.go4expert.com/forums

    Where the first argument is the folder or filename that you are trying to redirect people from, and where the second argument is the file or directory you are trying to get people to go to instead:

    Redirect old file/folder new file/folder

    The above line of code is a template for how the Redirect feature of .htaccess works.

    Changing Home Page/Directory Page



    When somebody types the URL to your site, more than likely, by default the index.htm or similar page will load first. You can actually use .htaccess and change which file gets loaded first when your URL gets loaded.

    DirectoryIndex file.htm

    In the above code, replace file.htm with the name of your new default page. When your visitors visit your site by simply typing in the URL of your site, they will be taken to whichever the default page you specify in the place of file.htm in the above code.

    IP Blocking



    If you would like to prevent an individual or a group of individuals on a certain IP range from accessing your site, you can selectively deny access to them.
    You would use the following command format:
    Code:
        <Limit GET HEAD POST>
        order allow,deny
        deny from 18.52.3.5
        deny from 18.132.152
        deny from 24.2
        allow from all
        </LIMIT>
    
    Of course, you would probably not be interested in blocking the fictitious IPs I mentioned above. You should change those to something more relevant. If you are really not well liked, you can add as many deny from lines of code as you want.

    If somebody from an entire IP range is bugging, you can block all IPs within that range by only entering a smaller portion of their IP such as 24.2. All IPs that being with 24.2 such as 24.2.35.3 and 24.2.142.122 will also be blocked automatically. You may want to be careful for you may not want to block access to visitors that you still want accessing site.

    Applying .htaccess to Folders and Sub-Folders



    Your .htaccess file doesn't need to be in the root directory, you can put it in any directory - it then applies to the directory you put it in, and all subdirectories.

    You can also allow only from certain IPs - for example:

    order allow,deny
    allow from 123.456.789.012
    deny from all

    Perhaps not so useful if you're applying it to your whole site, but good when used with other directories.

    The above .htaccess tricks are ones that I feel you will enjoy using. Remember, some server hosts may consider using .htaccess a violation of your privileges. Some HTML editing programs may have their own file management and access systems that do not play well when in the presence of .htaccess. Also, IIS servers do not support .htaccess.
     
  2. rapwaydown

    rapwaydown New Member

    Joined:
    Nov 28, 2007
    Messages:
    62
    Likes Received:
    0
    Trophy Points:
    0
    good articles, do you mind if i use this on my website, of course is creadit to you
     
  3. kien_vn

    kien_vn New Member

    Joined:
    Aug 31, 2010
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    This article is simple but effective for beginners.
     
  4. linux7802

    linux7802 New Member

    Joined:
    Sep 10, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.theperfectarts.com/
    Yes its useful for beginners as well as for professionals...
     
  5. PradeepKr

    PradeepKr New Member

    Joined:
    Aug 24, 2010
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.expertsguide.info
    This would be helpful in blocking bad bots/crawlers which eat up lot of bandwidth.
    Identify the IP of the bad bot and deny them :)
     
  6. parrytint

    parrytint New Member

    Joined:
    Aug 9, 2010
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.pacifictintlv.com/
    hi...

    How far the google shorten url is benefits like twitter..??? meet again.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice