Results 1 to 4 of 4

.htaccess tutorial and some useful examples

This is a discussion on .htaccess tutorial and some useful examples within the Member Articles forums, part of the Focus on Members category; The apache web server provides a feature called .htaccess file, which in a nutshell provides commands for a website. This ...

  1. #1
    Senior Member Array Jason_A's Avatar
    Real Name
    Jason Abbot
    Join Date
    Feb 2008
    Location
    USA
    Posts
    278
    Liked
    0 times
    Blog Entries
    1

    .htaccess tutorial and some useful examples

    The apache web server provides a feature called .htaccess file, which in a nutshell provides commands for a website. This file is simply a text file containing Apache directives.

    Warning: Be very careful when editing the .htaccess file, as one single mistake can stop your site working
    .

    All you need is a text editor, but I strongly recommend you use something other than notepad. All commands must be placed one one line, so if your text editor as word-wrap enabled, be sure to disable it. Also be sure to upload your .htaccess file in ASCII format, and not binary, otherwise it won't work.

    Usuall, your text editor or operating system won't allow you to save a file as .htaccess. If this is the case, just save it as htaccess.txt and upload it to your webserver using your FTP client. After doing that, you should use your FTP client and rename the file to it's correct name.

    Also keep in mind that some sites do NOT allow .htaccess files, since they can slow down a server, and some things that they can do can compromise a server configuration that has been specifically setup by the admin. Be sure to ask your host if it is allowed, or if they will allow you to have it enabled - ask and you shall receive.

    Below are some useful .htaccess command examples...

    Redirects

    You can use a .htaccess file to re-direct a specific page, to a new page:
    Code:
    Redirect /OldDir/old.html http://site.com/NewDir/new.html
    Custom error pages

    These are very useful for those who make mistakes in typing the url. The most common errors are 404 (page not found) and 500 (internet server error). You can design your site for these errors, by simple adding a command to your .htaccess file... you can use as many as you want for any page you wish, just add the following to your .htaccess file:

    Code:
     ErrorDocument 404 /404.html
    ErrorDocument 500 /500.htm
    The initial slash in the directory location represents the root directory of your site. You can name these errors anything you want, and anywhere within your site.

    If you don't want your site to re-direct, and you want it to display a "404 page not found" page, then I suggest you take a look at Keith Cohen great guide HERE.

    Enabling SSI

    Want to use SSI, but your current host doesn' allow it? Well, you can change that with your .htaccess file. The following lines tell the server that any file named .shtml should be parsed for server side commands:
    Code:
    AddType text/html .shtml
    AddHandler server-parsed .shtml
    Options Indexes FollowSymLinks Includes
    If you don't care about the performance hit of having all .html files parsed for SSI, change the second line to:

    Code:
     AddHandler server-parsed .shtml .html
    Protecting your bandwidth from hot linking

    We all know there are people out there who love to steal bandwidth, also known as "hot linking,". This is is linking directly to non-html objects on another server, such as images, etc. The most common practice of hot linking pertains to another site's images. To disallow hot linking on your server, create a .htaccess file with the following command inside, and upload it to the folder of images you wish to protect:

    Code:
     RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?YourSite\.com/.*$ [NC]
    RewriteRule \.(gif|jpg)$ - [F]
    Replace "Yoursite.com" with your own domain. That will cause a broken image to be displayed if it is hot linked. If you would like to display an alternative image in place of the hot linked one, replace the last line with the following:
    Code:
    RewriteRule \.(gif|jpg)$ http://www.YourSite.com/hotlink.gif [R,L]
    Replace "Yoursite.com" with your own domain and "hotlink.gif" with your image name.Redirect yoursite.com to www.yoursite.com

    This is very useful. If search engines find both www and non-www links from other sites to your site, then they may treat http://YourSite.com and http://www.YourSite.com as two different websites with the same content. This means you can get penalized for duplicate content.

    Therefore, I recommend you set-up a permanent 301 redirect from YourSite.com to www.YourSite.com:
    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^YourSite\.com [nc]
    RewriteRule (.*) http://www.YourSite.com/$1 [R=301,L]
    Again, replace "Yoursite.com" with your own domain.

    Prevent viewing of .htaccess or other files

    To prevent users from seeing the content of your .htaccess file, place the following code within it:

    Code:
    <Files .htaccess>
       order allow,deny
       deny from all
       </Files>
    If you want to prevent visitors from seeing another file, just substitute that file's name for .htaccess in the Files specification.


    Change the default directory page

    Usually, the default directory page is index.php, index.html ect. Many servers allow servers allow a range of pages called index, with a variety of extensions, to be the default page.


    What to change it to your very own for whatever reason? For example; name.html... no problem, just add the following to your .htaccess file for that directory:

    Code:
    Directory Index name.html
    I will add more in time, but there are enough for the time being.

    Thanks for reading!

    Regards Jason
    Last edited by Jason_A; 03-04-2008 at 10:29 AM.

  2. #2
    Senior Member Array
    Real Name
    Joseph Ward
    Join Date
    Jun 2005
    Posts
    23,845
    Liked
    44 times
    Blog Entries
    9
    Great article. Thank-you Jason.

  3. #3
    Senior Member Array Jason_A's Avatar
    Real Name
    Jason Abbot
    Join Date
    Feb 2008
    Location
    USA
    Posts
    278
    Liked
    0 times
    Blog Entries
    1
    Not a problem at all Joe.

    Regards Jason

  4. #4
    Senior Member Array
    Real Name
    Joseph Ward
    Join Date
    Jun 2005
    Posts
    23,845
    Liked
    44 times
    Blog Entries
    9

Similar Threads

  1. Hotlink Protection Tutorial (Apache Server - .htaccess files)
    By NeutralizeR in forum Member Articles
    Replies: 15
    Last Post: 06-08-2011, 02:25 PM
  2. Looking for tutorial
    By ChefTalk in forum General Discussion
    Replies: 2
    Last Post: 04-17-2007, 02:26 PM
  3. Deep Adsense Tutorial
    By usearchme in forum Ad Networks
    Replies: 0
    Last Post: 03-28-2007, 11:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •