In this tutorial you will learn about .htaccess and the power he has to improve your site. .htaccess is a single file, it can change the settings on servers and allows you to do many different things, the most popular being able to have your own 404 error pages. .htaccess is not difficult to use and is essentially composed of only a few simple instructions in a text file.

There are a variety of things htaccess can do, including: Password protecting folders, redirecting users automatically, custom error pages, change your file extensions, banning users with certian IP addresses, allowing users to only certain IP addresses, stopping lists directory and use a different file as the index file.

Before starting to use .htaccess, I’ll give you a warning. Even using .htaccess on your server is extremely unlikely to cause you problems. If you want (not recommended but possible), you must download it .htaccess file from your server, then add your code at the beginning.

Redirect To Other Domain
Redirect 301 / http://newdomain.com/
Rewriting [ product.php?id=1 to product-1.html ]
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
Rewriting [ yoursite.com/user.php?username=abc to yoursite.com/abc ]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
Redirect non www to www version of site
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Redirect www to non www version of site
Options +FollowSymLinks 
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
Move Single Page
Redirect 301 /oldpage.html http://www.example.com/newpage.html
Detect And Redirect iPhone Users
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteRule .* http://iphone.example.com/ [R]
Custom Error Documents
ErrorDocument 401 /err/401.php
ErrorDocument 403 /err/403.php
ErrorDocument 404 /err/404.php
ErrorDocument 500 /err/500.php
Custom Index page
DirectoryIndex index.html
Disable Hot Linking
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?corz\.org/ [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(gif|png) [NC]
RewriteRule .*\.(gif|jpg|png)$ http://example.com/hotlink.png [NC]