The apache web server provides a feature called .htaccess file, which in a nutshell provides commands for a website. This file is simply a text file containing Apache directives.
Warning: Be very careful when editing the .htaccess file, as one single mistake can stop your site working.
All you need is a text editor, but I strongly recommend you use something other than notepad. All commands must be placed one one line, so if your text editor as word-wrap enabled, be sure to disable it. Also be sure to upload your .htaccess file in ASCII format, and not binary, otherwise it won't work.
Usuall, your text editor or operating system won't allow you to save a file as .htaccess. If this is the case, just save it as htaccess.txt and upload it to your webserver using your FTP client. After doing that, you should use your FTP client and rename the file to it's correct name.
Also keep in mind that some sites do NOT allow .htaccess files, since they can slow down a server, and some things that they can do can compromise a server configuration that has been specifically setup by the admin. Be sure to ask your host if it is allowed, or if they will allow you to have it enabled - ask and you shall receive.
Below are some useful .htaccess command examples...
Redirects
You can use a .htaccess file to re-direct a specific page, to a new page:
Custom error pagesCode:Redirect /OldDir/old.html http://site.com/NewDir/new.html
These are very useful for those who make mistakes in typing the url. The most common errors are 404 (page not found) and 500 (internet server error). You can design your site for these errors, by simple adding a command to your .htaccess file... you can use as many as you want for any page you wish, just add the following to your .htaccess file:
The initial slash in the directory location represents the root directory of your site. You can name these errors anything you want, and anywhere within your site.Code:ErrorDocument 404 /404.html ErrorDocument 500 /500.htm
If you don't want your site to re-direct, and you want it to display a "404 page not found" page, then I suggest you take a look at Keith Cohen great guide HERE.
Enabling SSI
Want to use SSI, but your current host doesn' allow it? Well, you can change that with your .htaccess file. The following lines tell the server that any file named .shtml should be parsed for server side commands:
If you don't care about the performance hit of having all .html files parsed for SSI, change the second line to:Code:AddType text/html .shtml AddHandler server-parsed .shtml Options Indexes FollowSymLinks Includes
Protecting your bandwidth from hot linkingCode:AddHandler server-parsed .shtml .html
We all know there are people out there who love to steal bandwidth, also known as "hot linking,". This is is linking directly to non-html objects on another server, such as images, etc. The most common practice of hot linking pertains to another site's images. To disallow hot linking on your server, create a .htaccess file with the following command inside, and upload it to the folder of images you wish to protect:
Replace "Yoursite.com" with your own domain. That will cause a broken image to be displayed if it is hot linked. If you would like to display an alternative image in place of the hot linked one, replace the last line with the following:Code:RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.)?YourSite\.com/.*$ [NC] RewriteRule \.(gif|jpg)$ - [F]
Replace "Yoursite.com" with your own domain and "hotlink.gif" with your image name.Redirect yoursite.com to www.yoursite.comCode:RewriteRule \.(gif|jpg)$ http://www.YourSite.com/hotlink.gif [R,L]
This is very useful. If search engines find both www and non-www links from other sites to your site, then they may treat http://YourSite.com and http://www.YourSite.com as two different websites with the same content. This means you can get penalized for duplicate content.
Therefore, I recommend you set-up a permanent 301 redirect from YourSite.com to www.YourSite.com:
Again, replace "Yoursite.com" with your own domain.Code:RewriteEngine On RewriteCond %{HTTP_HOST} ^YourSite\.com [nc] RewriteRule (.*) http://www.YourSite.com/$1 [R=301,L]
Prevent viewing of .htaccess or other files
To prevent users from seeing the content of your .htaccess file, place the following code within it:
If you want to prevent visitors from seeing another file, just substitute that file's name for .htaccess in the Files specification.Code:<Files .htaccess> order allow,deny deny from all </Files>
Change the default directory page
Usually, the default directory page is index.php, index.html ect. Many servers allow servers allow a range of pages called index, with a variety of extensions, to be the default page.
What to change it to your very own for whatever reason? For example; name.html... no problem, just add the following to your .htaccess file for that directory:
I will add more in time, but there are enough for the time being.Code:Directory Index name.html
Thanks for reading!
Regards Jason![]()


LinkBack URL
About LinkBacks






Reply With Quote