Mod Rewrite Wanna know how to add mod rewrite to your site?
Mod rewrite example:
Tranform
index.php?view=tutorials&category=PHP into
/tutorials/PHP/ Wanna know how? Lets do it!
Heres an example of basic mod rewrite:
RewriteEngine On
RewriteRule ^(.*)/$ index.php?view=$1 [L]
That look complicated? It may do... Its not!
Now let me explain...
RewriteEngine On Telling the server to start mod rewrite
RewriteRule Start a new rewrite rule
^ You should include this in each rule, where it is positioned above in my example code.
Now we get onto the confusing bit.
(.*) Means contains anything. Also known as a wildcard.
/ means a forward slash in the URL
$ ends the new url in the rule
You can use other things instead of (.*), such as:
- [a-z] - Containing lowercase letters between a & z
- [A-Z] - Uppercase letters between A & Z
- [A-Z][a-z] - Lowercase & uppercase letters between Aa & Zz
- [0-9]- You can also use numbers, between 0-9
You do not have to add [L] at the end of every rule
So if you wanted to tranform index.php?view=tutorials&category=PHP into /tutorials/PHP/ you would need this:
RewriteEngine On
RewriteRule ^tutorials/$ index.php?view=tutorials [L]
That would do the /tutorials/ part. Now if we want to add the category, add another rule
RewriteRule ^tutorials/(.*)/$ index.php?view=tutorials&category=PHP [L]
When using multiple rules, its best to have the longest rule at the top. You do not have to, but it makes your code much clearer, and easier when your looking for something
Now to use this, you need to have mod rewrite enabled on your server. If you are not sure, ask your host .
If you want this code in a zip, its available at the end of the tutorial.
Now thats all there is to mod rewrite.