URL rewritingmod_rewrite is used for rewriting a URL at the server level, giving the user output from a different location. For example a user may ask for http://www.somesite.com/widgets/blue/, but will really be given http://www.somesite.com/scripts/widget.php?colour=blue and the user will be none the wiser. Some benefits of URL rewriting are:
URL rewriting is not the same as redirection. For example if you want a user going to Address_A to get the page at Address_B,
You can define the way you want mod_rewrite to behave in an .htaccess file. Rewrite engineTo make any of this work, you first need to ensure that mod_rewrite is switched on RewriteEngine on You only need to include Rewrite rulesAny number of rules can be included, and they take the following form: RewriteRule [] A URL that matches will be rewritten as . The magic is that you can use regular expressions in the and references in the strings. For example: RewriteRule /products/([0-9]+) /siteengine/products.php?id=$1 With this rule, can use following address: http://mydomain.com/products/123 and this will be rewritten to http://mydomain.com/siteengine/products.php?id=123 There's no way for your visitors to find out where your script resides (/siteengine in the example), what its name is (products.php), or the name of the parameter to pass (id)!
These are the most common flags - a full list of flags is given in the Apache mod_rewrite manual. Rewrite conditionsYou can also customise your rewriting using conditions. The format of a condition is: RewriteCond Any rewrite condition affects the behaviour of the following RewriteRule. It works like this: mod_rewrite takes all the RewriteRules and starts matching the current URL against each RewriteRule pattern. If there's a RewriteRule pattern that matches the URL, mod_rewrite checks if there are existing conditions for this RewriteRule, and if the first one returns true. If it does, the proper substitution will occur, but if not, mod_rewrite looks for remaining conditions. When there are no more conditions, the subsequent RewriteRule is checked.
For example, the following will direct the user to a different page depending on their browser: RewriteCond %{HTTP_USER_AGENT} ^Mozilla.* When a browser requests the index page, 3 things can happen:
There are endless possibilities, including IP or time-dependent conditions. |
Powered by WHMCompleteSolution