Sunday, April 5, 2009

.htaccess Rewrite Engine Tutorial Guide

Found this .htaccess rewrite engine tutorial very useful from netfirms.com
Keep this as a reference for myself, it might be useful to you too.

How do I configure URL forwarding using mod_rewrite?

To configure URL forwarding using mod_rewrite:

I. ENABLE .HTACCESS
  1. Login to the Netfirms Control Panel at https://controlpanel.netfirms.com
  2. Click Site Tools
  3. Click .htaccess
  4. Enable .htaccess
II. CONFIGURE YOUR .HTACCESS FILE USING REWRITE ENGINE (mod_rewrite)

Example #1:
The following converts http://yourdomain.com/xyz.html into http://yourdomain.com/index.php?xyz):

RewriteEngine on
RewriteBase /
RewriteRule ^([a-z]+).html$ /index.php?$1 [R,L]

NOTE: The RewriteBase cannot be empty. It must indidcate the base directory or a referring directory.

Example #2:
The following converts http://yourdomain.com/directory/xyz.html into http://yourdomain.com/directory/index.php?xyz):

RewriteEngine on
RewriteBase /directory
RewriteRule ^([a-z]+).html$ /index.php?$1 [R,L]

Example #3:

RewriteEngine On
RewriteRule ^tutorials/(.*)/(.*).php /tutorials.php?req=tutorial&tut_id=$1&page=$2

Example #4:
The following will display contents in newpage.html when visitor browses to oldpage.html:

RewriteEngine on
RewriteRule ^oldpage.html$ /newpage.html

Example #5:
The following will rewrite the URL to
http://yourdomain.com/directory/newpage.html when visitors browse to http://yourdomain.com/anypage.html

RewriteEngine on
RewriteRule ^([a-z]+).html$ /directory/newpage.html [R,NC,L]

The following will display contents of http://yourdomain.com/folder1/folder2/folder3/index.php when visitors browse to http://yourdomain.com/folder1/index.php (while maintaining http://yourdomain.com/folder1/index.php in the address bar of the browser)


RewriteEngine On
RewriteBase /folder2/folder3/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index.php
RewriteRule (.*) /index.php/$1 [L]
RewriteRule ^$ /$1 [L]

Example #6:
The following example will forward a visitor coming from a specified IP address (eg. 123.45.67.8) to another location (eg. /):

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^123.45.67.8$
RewriteRule ^.*$ - [F]

The following example will forward a visitor coming from a specified IP address (eg. 123.45.67.8) to another location (eg. /) if they try to access any .php files:

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^123.45.67.8$
RewriteRule .php$ - [F]

Another example of a similar .htaccess file using ip blocking in RewriteCond directive will forward user to http://www.yahoo.com URL if they come from a specified IP address (eg. 123.45.67.8):

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^123.45.67.8$
RewriteRule ^(.*)$
http://www.yahoo.com [L]

Example #7:
Hot linking (ie. bandwidth stealing) happens when people link to files and images on a different server, display them on their website and the bandwidth is at the other website owner's expense. The following example can prevent hot linking to your website:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?yourdomain/.*$ [NC]
RewriteRule .(gif|jpg)$
http://yourdomain.com/image.gif [R,L]

Other Examples of .htaccess configurations:

Example #8:
If you have problems with certain visitors to your website, you can ban them. There are two different ways to ban visitors: (1) using their IP address they came from or (2) using the domain name which they came from.

The following example denies a visitor coming from IP address 123.45.67.8:

order allow,deny
deny from 123.45.67.8
allow from all

The following example denies visitors coming from a block of IP addresses coming from 123.45.67.0 to 123.45.67.255

order allow,deny
deny from 123.45.67.
allow from all

The following example denies a user by the domain name from which they came from:

order allow,deny
deny from www.theirdomain.com
allow from all

The following example denies a visitor from a domain name and all subdomains within the domain name:

order allow,deny
deny from .theirdomain.com
allow from all

The following example denies all visitors except your own IP address assigned to you by your ISP (Internet Service Provider):

Order deny,allow
Deny from all
Allow from youripaddress

Example #9:

The following example denies access to all files if the visitor is browsing from IP address 123.45.67.8:


Order allow,deny
Deny from 123.45.67.8
Satisfy All

Example #10:
You can configure .htaccess to redirect a visitor visiting a non-existing asp page to a custom error page as long as your hosting plan supports Windows hosting (eg. Business hosting plans and higher). Since our webservers are Windows aware, it tries to process the asp script before making a determination whether the asp file exists or not. As such, using htaccess will not work to redirect them to a custom error page because it will simply err with Service Unavailable (on ADV plan, ASP is not supported). That is, the redirection will not work if the (asp) file does not exist. However if on a plan with Windows support the redirection will work with the following similar coding in .htaccess file:

RewriteEngine on
RewriteBase /
RewriteRule ^([a-z]+).asp$ /directory/customerrorpage.html [R,NC,L]

Example #11:
The following example will redirect a user browsing to yourdomain.com to www.yourdomain.com:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com
RewriteRule ^(.*)$ http://www.yourdomain.com [R=permanent,L]

Example #12:
The following example redirects all requests to the new domain saving the URI in the variable and passing it through to the new domain:

RewriteEngine ON
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]


Note: Once your .htaccess file has been created you may upload it to the remote server via FTP. Please ensure that the .htaccess is uploaded in ASCII mode if your are using an FTP client such as filezilla or wsftp.

Example #13:
The following example redirects all http:// requests to https:// assuming a dedicated SSL certificate has been installed and activated:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} foldername
RewriteRule ^(.*)$ https://www.domain.com/foldername/$1 [R,L]

Example #14:
The following example allows visitors to browse to .html pages and parse them as PHP pages (.html will be retained in the URL):

RewriteEngine On
Rewritebase /
RewriteRule ^index.html$ index.php [L,NE]
RewriteRule ^(.*).html $1.php [QSA,L]

Example #15:

The following example will allow all of files in .php extension to be accessed using .html or .htm extension:

RewriteEngine On
RewriteRule (.*).htm$ $1.php [NC,L]


TROUBLESHOOTING:
We recommend that you make small changes to .htaccess saving frequently, and testing your site. That way, you'll know when you've made a mistake, and will be able to quickly roll the change back. If after you configure an .htaccess file your website displays a 500 Internal Server Error, please review your newly created .htaccess file to ensure that you have followed the above instructions properly.

For further instructions on how to configure .htaccess with the above supported commands, we recommend browsing to www.apache.org or httpd.apache.org/docs/misc/rewriteguide.html

No comments: