Aresss
12-18-2006, 04:00 PM
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!
Code:
Options -Indexes 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.
Code:
AddType application/x-httpd-php .php .htmThis 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, and also I just happen to like it more
Code:
ErrorDocument 400 /errors/400.htm
ErrorDocument 401 /errors/401.htm
ErrorDocument 403 /errors/403.htm
ErrorDocument 404 /errors/404.htm
ErrorDocument 500 /errors/500.htm 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.
Code:
redirectPermanent /somepage.htm http://www.example.com/index.htmJust 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.
Code:
redirectMatch 301 ^/old_directory/(.*) http://www.example.com/index.htmHere 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.
Code:
redirectMatch 301 ^/subsite/(.*) http://www.example.com/$1
redirectMatch 301 ^/subsite http://www.example.com/$1Say 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.
Code:
Options +FollowSymLinks
RewriteEngine onThis code gets the server ready for the next few things we will do
Code:
RewriteCond %{HTTP_HOST} ^example.(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 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.
Code:
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.example.com/index.htm [R,NC]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:
Code:
RewriteEngine offThis will counter-act the global .htaccess, and allow hot-linking from whatever directory you stick it in (and sub-directories)
Code:
Options -Indexes 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.
Code:
AddType application/x-httpd-php .php .htmThis 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, and also I just happen to like it more
Code:
ErrorDocument 400 /errors/400.htm
ErrorDocument 401 /errors/401.htm
ErrorDocument 403 /errors/403.htm
ErrorDocument 404 /errors/404.htm
ErrorDocument 500 /errors/500.htm 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.
Code:
redirectPermanent /somepage.htm http://www.example.com/index.htmJust 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.
Code:
redirectMatch 301 ^/old_directory/(.*) http://www.example.com/index.htmHere 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.
Code:
redirectMatch 301 ^/subsite/(.*) http://www.example.com/$1
redirectMatch 301 ^/subsite http://www.example.com/$1Say 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.
Code:
Options +FollowSymLinks
RewriteEngine onThis code gets the server ready for the next few things we will do
Code:
RewriteCond %{HTTP_HOST} ^example.(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 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.
Code:
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.example.com/index.htm [R,NC]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:
Code:
RewriteEngine offThis will counter-act the global .htaccess, and allow hot-linking from whatever directory you stick it in (and sub-directories)