This is a beginner's guide to htaccess. It is meant to introduce this configuration file and its useful implementations in website management. .htaccess is a server configuration file commonly found in Apache, Zeus and Sun Java System web servers. It provides a lot of applications with implications for website security and search engine optimization. You cannot find or use .htaccess in Microsoft-based IIS servers and non-Apache-based servers.
To create and upload an htaccess file:
1. Open any text editor (such as geditor in Ubuntu or notepad in Windows).
2. Enter the htaccess syntax you would like to implement on your website (at the text file).
3. Save it as .htaccess
Note: There is a "." before the filename, and there is no file extension to it; just .htaccess
4. Upload it to a directory where you would like the directives to be implemented. An htaccess uploaded to the root directory of your website will cover all of the website's directories and files (from root to inner).
But if you upload only the .htaccess file to a specific inner directory, the directives on that .htaccess file can be only be implemented in that specific directory.
IMPORTANT: If you have an existing .htaccess in your website and you plan to edit it, it is extremely important to secure a backup of that .htaccess first, before doing anything else.
301 redirecting old/dead URL to new URL
Suppose you have URLs, and then you delete them because they are no longer needed. You need to replace them with new URLs (by re-publishing content, for example, or shortening your URLs).
With respect to SEO implementation, this approach can cause you to lose a lot of traffic and link juice, because those old URLs might be contributing a very significant amount of traffic and links to your website.
What is the best solution? Instead of giving those URLs a 404 header status (not found, or does not anymore exist), you need to 301 redirect those non-existing URLs to their new, permanent location.
With this method, if those dead URLs are still indexed by Google, any users coming from search engines or other websites can still read and visit the new URLs because they have been "301 redirected."
To do this, you can use .htaccess to do the 301 redirects. For example:
The above line will 301 redirect that URL with spaces to this new location: http://www.php-developer.org/excel-database-sample-sheets/
Note: 301 redirection implementations using .htaccess will be uploaded to the root directory of your website. If you have an existing .htaccess, you will simply need to edit it and add the redirection lines. You will not need to actually create a brand-new .htaccess.
Disable hot linking to save website bandwidth
If you have a website in which you are hosting fairly large files like MP3s, video files and audio wave files, then someone outside your domain might be tempted to take advantage of your website's resources by directly streaming/linking to it, without paying a single penny for bandwidth use.
Why is this not good? Your website's bandwidth (for which you are paying with your hosting bills) is being substantially consumed by unauthorized persons. This is known as "bandwidth theft" and includes stealing pictures from your website by reusing them in another website (NOT in your website).
Abuse can slow down your site and weaken your website security. This is where you can use .htaccess to prevent this.
For example, say you want to prevent "hot linking" for the following file types mp3, jpg and wav audio files. You want to allow only your own domain (e.g yourdomain.org) to access them; other domains are restricted.
Again, if you upload the .htaccess file to the root directory of your website, all of your website directories and files will be protected against hot linking, as stated in your .htaccess file.
Adding more than one domain to hot link to your site
There are times when you have several websites and you need to hotlink to the images, perhaps for convenience and saving disk space. In this case, if you implemented the above .htaccess syntax, then your website will not be permitted to access those images.
You can grant permission to any website to access and "hot link" your web content (images, etc). For example: