and and .
.htaccess is a processing file that controls Apache web servers and is used in conjunction with mod_rewrite which is a rewrite engine used by web servers to modify urls before they load. You cannot use .htaccess unless your hosting has an Apache web server.
The .htaccess file is used to change code or the way a page is displayed before the page code is downloaded by a user, so it can alter the page code or the way a server would normally process page code.
The .htaccess file has no text before the . period, it is only an extension. Sometimes when you upload it to a server you may be told that it is an invalid filename, in which case remame it temp.htaccess in your computer and upload that file, then rename it on the server.
The .htaccess file is required in the root directory on your hosting service server and often in sub-directories, for instance where you have an add-on domain which your hosting service has pointed to a particular sub-directory. This may happen if you have a .com domain as your main domain but also a .net add-on domain in the same hosting account and the .net domain may be hosted as my-domain.com/sub-domain-name/ or sub-domain-name.my-domain.com either of which will be loaded from my-domain.net.
In many cases your hosting service control panel will have menu buttons for the most common .htaccess features so you only have to insert basic data like a url or choose options and the .htaccess file will be coded automatically and inserted in the correct directory or directories.
The code in a .htaccess page should be just plain text, no comments or tags.
The items below are just some of the common uses of a .htaccess file, but there are many other uses.
and and .
1 Canonicalization is an unusual word but means that search engines will treat different urls as different pages, resulting in a lower ranking for each, even though the different urls lead to the same page. Make sure you have no canolicalization issues for your homepage.
are all seen as different urls, but they probably have the same content in most cases. Google can generally decide which version to use but you can still run into problems.
For typical .htaccess code, see.
2 You may have started your page coding some time ago with a .html extension without PHP and later decided to add PHP, but by then people will have bookmarked the page with the .html extension but you now want to change it to a .php extension which would make all the bookmarked urls invalid. You can use .htaccess to tell the server to serve all .html pages as.php so that previous pages with .html extension can still be valid but will process the PHP.
AddHandler application/x-httpd-php .html
The above code will apply to all .html pages; even if they have no PHP code it won't matter if they are processed as if they had PHP. You can adapt the code for other extensions.
3 Redirect one page to another:-
redirect 301 /old-page.php http://www.your-site.com/new-page.php
4 If you have a page filename that is excessively long or contains dynamic PHP you may wish to shorten it to something easy to type or remember easily.
The Apache rewrite engine can be used to turn dynamic urls such as www.your-site.com/ford.php?id=fiesta into static and user friendly urls such as www.your-site.com/ford/fiesta
Or rewrite from:-
www.your-site.com/cars.php?ford=fiesta to www.your-site.com/cat/ford/fiesta/
RewriteRule cat/(.*)/(.*)/$ /cars.php?$1=$2
This method can also be adapted to delete the filename from a url so that only the domain url or the domain name plus a sub-directory form the url:-
http://www.your-site.com/folder1/item1.html could be changed to:-
5 Hotlinking happens when another website uses code to link to an image in your server using:-
<img src="http://www.your-site.com/images/image1.jpg" alt="image 1">
Any person loading the other site will immediately download the image from your server, using your bandwidth and effectively stealing your image, without showing that the image comes from your website.
You can use .htaccess code to block any images downloaded by hotlinking or you can substitute another image with a suitable message embedded in it like "Don't steal bandwidth" or use an image of an angry man.
The above hotlinking is not the same as another website using a hyperlink to one of your images with this code:-
<a href="http://www.your-site.com/images/image1.jpg">Image 1</a>
which is only activated if the viewer of the other site actually wants to see the image and clicks on the link and the viewer is taken to a separate page from your web server that will show your url in the browser address bar.
For typical .htaccess code for hotlinking protection, seeand and .
Stealing images is a constant problem on the net. The internet was set up originally to allow free access to content and code, and that is still generally true. Images can be made difficult to download and save by amateurs but experts can nearly always find a way to do it. Various ways to make copying images difficult include:-
See detailed methods for preventing image copying in
If permission is given to someone to use your image on another website, the other site should have text on the same page indicating where the image is sourced and acknowledge that copyright is owned by you.
6 You can use .htaccess to password protect a directory of your website.
For a .htaccess code generator, see.