|
.htaccess tutorialTOC - Table of Contents: Introduction to .htaccessIf you are hosting your site on a shared server (virtual hosting), then you don´t have access to the basic configuration of the servers on which your websites are hosted. But there are certain functions which require a different server configuration, so there is the possibility for you to override the default server configuration using certain commands in the so-called .htaccess file. ".htaccess" is a Unix file format and can only be used on linux (apache) servers. Some examples of what can be done with a .htaccess file. They are all covered in this tutorial:
Please note that not all webhosts allow their users to use .htaccess files, so if you are unsure it would be good to ask your host if they do or simply check their support forum / knowledge base for discussions about how to use .htaccess files. How to create .htaccess filesCreating a .htaccess file is very easy and can be done with any ASCII text editor like textpad or notepad. I´ve noticed that many people are confused about the strange filename. Well - actually this file doesn´t even have a name at all, it´s just a long file name extension - .htaccess. Hence, the DOT before "htaccess" is NOT a typo. To create a .htaccess file, simply open a new blank document in your favorite text editor and save the file as .htaccess. Some text editors (like notepad) do not support the Unix file format and therefore you won´t be able to save a file with the ".htaccess" extension only. In this case you can simply save the file as 1.htaccess or htaccess.txt and later (after you have uploaded it to your server) rename it to .htaccess through your FTP client. Create server side redirectsServer side redirects are very useful for three main purposes: 1. When you move a site to a new domain, you should create permanent redirects (301 redirects) from the webpages of your old domain to the new domain, not only to redirect your visitors to the new domain but also to tell the search engines that your site has moved permanently. This way you won´t lose your rankings and also pass on your old website´s PageRank to the new site. In the .htaccess file of the old domain you would put the following line to redirect all traffic from
the old pages to the according new pages on your new domain. 2. When you promote an affiliate program, especially inside an email message, your affiliate links not only look ugly in most of the cases but also leave you vulnerable to commission theft, especially ClickBank affiliate links. It´s just too easy to recognize an affiliate link as such and in case of the ClickBank links, users can simply replace your nickname with theirs and steal your commission. To prevent this from happening and to make your affiliate links look better, you can create either
temporary redirects (302 redirects) or permanent redirects (301 redirects) in your .htaccess file - for example: To create these redirects, I need to add the following lines to my .htaccess file:
Redirect temp /googlecash http://hop.clickbank.net/?stiehle/googlecash You can also use 302 instead of temp
Redirect 302 /googlecash http://hop.clickbank.net/?stiehle/googlecash To create the permanent redirects instead of the temporary ones, simply replace temp with permanent and replace 302 with 301. For more info and links to discussions about the pros and cons of temporary and permanent redirects, read this post at the webmasterworld.com forum. 3. I also recommend that you create temporary redirects to resources on the web everywhere you place these links in places where you have no access in the future (f.ex. ebooks, reports, infoproducts). This way you are still in control over the target URL and you can modify the target URL in case the URL of the resource changes. Define custom error pagesYou can define your own error pages to improve the communication with your visitors
in case something´s wrong with your site or if they are trying to access pages that
are no longer available. Simply create a webpage for each of the error codes you like. Here are some of the
most common errors: You can name these custom error pages anything you like, but I recommend you choose file names that remind you of the function of each document (f.ex. notfound.html or 404.html for the 404 error page) and you can store them in any location on your server that has web access (either your root directory or a subdirectory). Then you can define these pages as custom error pages in your .htaccess file. Supposing you have named your error pages 401.html, 404.html and 500.html and stored them in your website´s root directory, then you would add these lines to your .htaccess file:
ErrorDocument 401 /401.html If you have named these pages "notfound.html", "authorization.html" and "internalerror.html" and uploaded them to a subdirectory named "errorpages", you would add this to your .htaccess file:
ErrorDocument 401 /errorpages/authorization.html Enable SSI on static webpages (.html and .htm)SSI (server side includes) provide a very flexible and easy way to update the content and style of certain parts (footer, header, sidebars, menus, ad blocks) of all your webpages by modifying a single file only. To learn more about SSI, read the SSI tutorial. To enable SSI on static .html and .htm webpages, you need to add the following line to your .htaccess file. Please note that this only works if your webhost supports SSI. AddHandler server-parsed .html .htm Enable PHP parsing on static webpages (.html and .htm)If you want to execute dynamic PHP code on otherwise static .html and .htm webpages, you need to enable PHP parsing on this type of webpage. To do this, simply add the following line to your .htaccess file and then you can include PHP code directly in the source code of your .html and .htm webpages. Make sure that you embed all PHP code inside opening and closing PHP tags (<?php and ?>). AddType application/x-httpd-php .html .htm Please note that you cannot use SSI and PHP parsing at the same time. But don´t worry:
you can include PHP code in SSI enabled webpages, too. For this purpose you would need to paste
the PHP code into a blank text document, save the file as whateveryoulike.php, upload it
to your server and then include it with SSI: Banning certain IP addresses from accessing your siteWhatever the reasons are, sometimes you may want to ban certain IP addresses from accessing your website(s). This can easily be achieved by adding the following lines to your .htaccess file (replace IP with the IP address you want to ban from accessing your site, f.ex. 123.33.64.1):
order allow,deny Of course, you can specify more than one IP addresses and also C or B class IPs - this way you would ban all IPs from the entire class. F.ex. deny from 125.30.5. would ban all the IPs that start with 125.30.5. (125.30.5.1, 125.30.5.2, etc ...). Add one IP per line in your .htaccess file like this:
order allow,deny Instead of banning by IP, you can also block access to your site by referring URL. For more info about this and detailed instructions, read this guide. How to password protect individual directories on your siteIf you are offering downloads from your site, you may want to place these into a subdirectory of your website and protect it with a password in order to prevent the downloads from unauthorized access. This way, you can allow access only for registered members or for your newsletter subscribers and also change the password from time to time. Most webhosts offer this password protection feature directly in their control panels, so I recommend you log into your control panel and define usernames and passwords for the directories that you want to protect there, because this requires more than just editing the .htaccess file.
|
||
Main Navigation |
|
|
Home :::
Contact :::
Advertise :::
Submit Tutorials :::
Links :::
Sitemap
|