.htaccess Tips

Discussion in 'Apache' started by elixir, Nov 29, 2005.

  1. #1
    I am going to try to make it somewhat useful Below I show some common uses of .htaccess which I use myself on many sites. Many of you will find no new information here, but hopefully it will help some!


    This causes the server not to list the contents of directories (when there is no index). This keeps people from typing in http://www.example.com/images/ (or any other directory) and getting a list of the files.


    The control panel of many hosts let you set the custom error pages, but doing it directly is just as easy. I use these 5 common ones. Just create your pretty error pages, and point to them in .htaccess.


    Just a basic redirect example here. For when you get rid of a page and want all links to it to just point to the index.


    Here we get a little fancier. This is for when you remove an entire directory. It will make any link to that directory (including all pages and sub-pages in it) go back to your main index.


    Say you have a sub-site on your domain.. in the 'subsite' directory, and you moved it to its own domain. These two redirects will take all possible link combinations and redirect them to the new domain. Even if the link is like this : /subsite/1/2/3/4/index.php it will redirect to www.example.com/1/2/3/4/index.php. The two lines give you better flexibility. It handles all combinations of trailing slashes, directories/files, and www. prefixes.


    This code tells your server to process PHP code in files with the .htm extension. Some servers allready do this by default, but many do not. You can also add .html to the end (or any other extension) if you like. This will keep you from having to rename files to php if you add scripts in later.


    This code gets the server ready for the next few things we will do


    This bit of code is quite usefull. It will take any links that do not have www in them, and add it to the URL. This means no matter how they link to you, your full domain (with the www) will get the backlink.


    This is basic hot-link protection code. I did have two other lines added to it (to take care of the times when the link does not contain the www) but if you use the last tip, you do not have to worry about it. Just make sure to put them in order (so the www gets added before the hot-link protection). This will only allow hot-linking from your site. If you want to add another site, just add the appropriate lines.


    Now if you want to sometimes allow hot-linking, here is a good method. Create a directory called 'share' (or whatever) and stick in a new .htaccess file with only this line:
    This will counter-act the global .htaccess, and allow hot-linking from whatever directory you stick it in (and sub-directories)

    Any questions? Corrections?
     
    elixir, Nov 29, 2005 IP
  2. vinod2005

    vinod2005 Banned

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Nice post thanx.
    I have a problem though.
    I created the the .htaccess file
    i uploaded it to my server. now how do i link to it.
    I would like to use this.
    -----
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
    ------
    or do i not need to do anything other than upload it
     
    vinod2005, Nov 30, 2005 IP
  3. The Big Deal

    The Big Deal Peon

    Messages:
    212
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You just need to upload it to the root of the site.
     
    The Big Deal, Nov 30, 2005 IP
  4. elixir

    elixir Banned

    Messages:
    72
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes,

    create htaccess with file manager on your server, ftp got some issue with transfering data without classic extension (.php, .asp and all other)

    all you have to do is to put this in your root folder (example: public_html or www or whatever you have)

    Many servers consider files with no prefix (file names starting with a period) to be hidden files.

    Some FTP programs give you the option to specify certain parameters for directory listings. I use CuteFTP, and I just have to go into the config and type in the " -a " parameter for the "ls" command. This way I get a full list of files on the remote server (including the htaccess file). Your FTP program probably has the same option somewhere. It makes it much easier :)
     
    elixir, Nov 30, 2005 IP