At least once a week I am approached with a rewrite or modification to someone else’s code. I decided to take on the most recent mod for a friend and am not enjoying it one bit. It is getting harder and harder for me to take on new projects because of garbage like this :
What the hell kind of form validation is that?!
My question is how does this pass company standards? How are people getting away writing this rubbish and being paid top dollar over someone who codes to standards and follows MVC.
I am definitely no PHP guru – but for god sakes start writing maintainable code people!
If you are wondering what inspired this rant – take a gander at this gimmicky product… I need a drink.
Mod Rewrite is an Apache module that can be used to rewrites URL’s to a “prettier” string in the clients browser.
A great
For the leet SEO guys mod_rewrite is a must. The problem for most is understanding exactly how it maps actions to page views.
For a basic setup you would start by creating a virtual host in /etc/httpd/conf/httpd.conf that looks something like this :
<VirtualHost>
ServerName yoursite
DocumentRoot “/var/www/yoursite”
DirectoryIndex index.php
<Directory /var/www/yoursite>
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
You would then need to create a .htaccess file in /var/www/mysite.
Lets say we have a simple controller to handle actions like :
<?php
function controller() {
// create a list of actions for this user level
$public_actions = array(”home”, :login:, “contact”);
// lists what the user can do, don’t forget to sanitize this string!
$action = $_GET[’action’];
// return an action if it exists in the list of actions the user can take
if (in_array($action, $public_actions)) {
return $action();
} else {
// if they are trying to play with your get string send them to the home //page
return home();
}
}
?>
So we have several actions the user can take.
* home
* login
* contact
If the get string contains any of these actions, the controller will direct the user to the corresponding function.
So to visit the contact page, the URL would look like
http://yoursite.com/index.php?action=contact
However that is ugly and we want it to look like
http://yoursite.com/contact.html
To accomplish this, we need to use mod_rewrite. Lets place the following in our document root’s .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.html$ /index.php?action=$1
Our link now works!
But what if there is more than one variable being passed through the get string? What if we were writing a blog and needed to view specific posts. We would have to specify the action and the post id or permalink.
We would need another rule to rewrite the URL, however we also need to specify a unique identifier as to which rul to follow, thats easy. Just add this next line to the bottom of your .htaccess :
RewriteRule view/(.*)/.*()\.html$ /index.php?action=$1&id=$2
In this case the variable $1 is the value of the action and $2 is the value of the post id. Notice the “view/” sets the identifier of this rule.
Now http://yoursite.com/view/posts/1.html should work.
The thing is google wont really like 2.html, it is just not very descriptive. So what I generally do is instead of mapping post id’s, I will ereg the title to replace special characters and replace with underscores, then insert that unique “permalink” into the database. I can now use a permalink to identify a specific post instead of the posts id number, hooray!
What if I need a rule to handle more get variables?
Create a new rule with another unique identifier such as :
RewriteRule archives/(.*)/(.*)\.html$ /index.php?action=archives&year=$1&month=$2
Now I can pass the URL a value of http://yoursite.com/archives/2008/january.html and it will pass the variables
$action => archives
$year => 2008
$month => january
Hope this tutorial helped!




