Skip to main content

Mod_Rewrite

To direct the URL http://www.yourdomain.com/index.htm to http://www.yourdomain.com/main.htm one can use the following code inside .htaccess file:

RewriteEngine on
RewriteRule ^index.htm /main.htm

This would cause http://www.yourdomain.com/index.htm to display the contents of http://www.yourdomain.com/main.htm while the browser would continue to show http://www.yourdomain.com/index.htm address.

However, if one has to direct a URL like http://www.yourdomain.com/resource/view.php?id=2 to
http://www.yourdomain.com/course/view.php?id=3 the following code does not work:

RewriteEngine on
RewriteRule ^
resource/view.php?id=2 /course/view.php?id=3

What needs to be done is as follows:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=2
RewriteRule ^resource/view.php /course/view.php?id=3

OR

RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=2$ [NC]
RewriteRule ^/?resource/view.php$ /course/view.php?id=3