![]() |
|
||||||
The CodeOpen up your .htaccess file. To begin using mod_rewrite, your first line of code will be:Simple enough, right? Next we're going to tell mod_rewrite where to work (base directory) with this statement: The next line of code will be your actual rewrite rule which contains regex. What is regex? "A regular expression, which is a way of describing a pattern to match in text. The directive definition will specify what the regex is matching against." - Apache See also: Apache.org When I started learning about mod_rewrite, this is the part I first had trouble with because I don't think I found it explained too clear anywhere, so here I am going to describe each part to you the best I can. you will use these expressions in the code to rewrite your urls, here they are and what they do: ^ - start of line,to match from beginning. $ - End of line, match to end. .(dot) - Match any single character (except for zero). * - Match many characters. [ ] - Match any character within the brackets. [^ ] - Match any character, except whats listed in brackets. + - Match the preceding element 1 more times. - excape preceding. .* - will match an unlimited number of characters. ok, that should clear things up a bit. At the end of this tutorial, I will provide links to some references and guides you can use to continue learning about regex, ect. At the end of some of your rules, you declare what exactly the rule is evaluating, see below: [NC] - No case (not case sensitive). [OR] - Or next condition (declare one, then put [OR] and declare another). [L] - Last rule. [F] - Force URL to forbidden (HTTP 403: Forbidden). [D] - Is a directory. [R] - Redirect to external redirection (HTTP 302: Moved Temporairly). You can use these combined with others. If you want to say no case for a rule, and specify an or, then you would just do [NC,OR] at the end. And if you use a ! in front of them, this will turn it around. Like, if you had [!L] would mean, Not a link, just like using ! in other programming. Now that we have turned the rewrite engine on, and declared the base directory, we can begin setting up our rewrite rules. Lets breakdown our rule. You see ^(.*) followed by categories.php?type=$1 and finally [NC]. ^(.*)/ is a wildcard, to which we refer back to with $1. This rule is saying, The first directory after /tutorials/ will now contain the text from whatever type equals in categories.php?type=. For example, If you link a visitor to: www.yoursite.com/tutorials/php/ ^^it will display the results as if you had send them to: www.yoursite.com/tutorials/categories.php?type=php See? Now, if php was not a type in your categories.php file, then it would bring up the error you set when an unknown type is attempted.
No Comments for this page. |
|
||||||||||||||||||||||||||||