Publishing System Settings Logout Login Register
Everything you need to know about .htaccess! Massive tutorial!
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on November 22nd, 2006
8408 views
General Website Development

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.

Next Page
Pages: 1 2 3 4 5 6 7 8 9 10
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Adam

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top