Monday, July 20, 2009

PHP - Mod Rewrite Quick Lesson

Before reading this blog entry and attempting to perform the same action, make sure that the extension mod_rewrite has been activated on your hosting account or server.

I
recently just figured out the Mod Rewrite Plug in. Mod Rewrite always a php apache server to have "user friendly" and "Google Friendly" urls. Instead of a page looking like http://egtworlds.com/turl.php?webid=41 a page could look like this http://egtworlds.com/t/41 much simpler and easy to understand.

In my case I remapped the links to the hosted websites, from being

http://egtworlds.com/mypage.php?type=website&webid=35&webdesc=Poppen.ca - Rock solid web design

To the simple and condensed version:

http://egtworlds.com/website/Poppen.ca - Rock solid web design/35

I'm still having some very small problems, but the jist is the same.

To start go to your root directory, where index.php (or index.html) is for your main site. You should find a file called ".htaccess," if not create it in notepad, and open it in notepad.

Next add the turn - on switch for the rewrite mod,
RewriteEngine on

Next is the Rewrite Base Directory. This is where you want the rewrite mod to start from, in most cases the main directory ("/") in other cases you may just have a huge file-tree that you want to condense like "/people/places/things/after-death/"

IMPORTANT REMEMBER THE BEGINING SLASH, otherwise your rewrite mod will not work.

RewriteBase /

The next step is to start rewriting links, from a freindly link of http://egtworlds.com/website/Poppen.ca - Rock solid web design/35 . Of course its simple:

This is what the mod line looks like, be sure to have each mod on a new line. Including the rewrite base, and turn-on line.

RewriteRule ^website/(.*)/(.*)$ http://egtworlds.com/mypage.php?type=website&webid=$2&webdesc=$1 [L]

BAM! It looks ugly, I know. Take a breather and let me walk with you.
  • RewriteRule is just the tag.
The first address is the expression for the link the user clicked on or typed in.
  • ^ tells the mod to start from the base, otherwise you can just list from the root directory, base is quicker for most cases.
  • website/ is my directory. You could have anything their, from html or whatever. IE My/lovely/website/
  • (.*) Indicates a value of unknown length. The first one is the name of my description.
  • / anything just a place holder.
  • (.*) Yet another value.
  • $ IMPORTANT says I'm finished the next group of text is my next address. This is required.
The second is the resulting address, where the stuff exists now.
  • http://egtworlds.com/mypage.php?type=website&webid= Text, can be anything
  • $2 is the reference to the second (.*) value. Their position based.
  • &webdesc= is another block of text
  • $1 is the reference to the first (.*) value. Their position based.
The last text "[L]" is just a flag, it's not that necessary. In my case it means, stop redirecting after this line. This stops an infinite loop from happening.

Enjoy, and remember to make the link the new cool user-friendly uri!


Site Comments:

Sponsored by EGT Worlds.

EGT Worlds Rating