Apache mod_rewrite is a tool that I wish I had tried to understand a lot early than I did. It makes URLs look so much cleaner not to mention the positive SEO results. This capability can take a URL that is common in PHP that looks like: http://www.yoursite.com/users.php?name=testuser&mod=settings and turn it into a URL that looks like: http://www.yoursite.com/users/testuser/settings.
The basics are fairly easy and straight forward. You will need to create a .htaccess file in the directory you want to format the URL. I always include in the first line of the file "RewriteEngine on". You can use "RewriteEngine off" to disable blocks of rules so that you don't have to comment them out or delete them from your .htaccess file. The RewriteRule directive uses Regular Expressions to match the URL typed to the URL you want.
Example:
RewriteEngine on
RewriteRule ^my/account([/]*)$ myacct.php
RewriteRule ^news/([0-9]+)$ news.php?id=$1
Explanation:
RewriteRule ^my/account([/]*)$ myacct.php: This rule looks for a URL starting and ending with "my/account" after the website, for instance http://www.yoursite.com/my/account and then changes it on the server side to be http://www.yoursite.com/myacct.php, but the user only sees http://www.yoursite.com/my/account.
RewriteRule ^news/([0-9]+)$ news.php?id=$1: This rule looks for a URL starting with "news" after the website. After "news" it looks for a number that corresponds with the id generated with the news posting. The "+" allows for one or more digit to be entered. For instance http://www.yoursite.com/news/1 would be changed on the serve to http://www.yoursite.com/news.php?id=1 , but the user only sees http://www.yoursite.com/news/1.
I hope this helps get you started. This is just a basic tutorial and there is so much more that can be done with Apache's mod_rewrite feature. For more in depth information, check out these resources:
Here is a function that I have developed in PHP for checking to see if data is in a valid format. These are some of the common validations I have needed while programming various projects and websites. Regular expressions are some of the most powerful validation checks available in PHP, however they are not always easy to understand.
This function is used by passing 2 arguments: the type of validation check you want to be performed, and the data you want validated. The function itself uses a simple switch statement (a quicker and cleaner if/then statement). The function simply returns a boolean True or False.
function isValid($type,$var) {
$valid = false;
switch ($type) {
case "IP":
if (ereg("^([0-9]{1,3}\.){3}[0-9]{1,3}$",$var)) {
$valid = true;
}
break;
case "Email":
if (ereg("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$",$var)) {
$valid = true;
}
break;
case "URL":
if (ereg("^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu)$",$var)) {
$valid = true;
}
break;
case "SSN":
if (ereg("^[0-9]{3}[- ][0-9]{2}[- ][0-9]{4}|[0-9]{9}$",$var)) {
$valid = true;
}
break;
case "CC":
if (ereg("^([0-9]{4}[- ]){3}[0-9]{4}|[0-9]{16}$",$var)) {
$valid = true;
}
break;
case "ISBN":
if (ereg("^[0-9]{9}[[0-9]|X|x]$",$var)) {
$valid = true;
}
break;
case "Date":
if (ereg("^([0-9][0-2]|[0-9])\/([0-2][0-9]|3[01]|[0-9])\/[0-9]{4}|([0-9][0-2]|[0-9])-([0-2][0-9]|3[01]|[0-9])-[0-9]{4}$",$var)) {
$valid = true;
}
break;
case "Zip":
if (ereg("^[0-9]{5}(-[0-9]{4})?$",$var)) {
$valid = true;
}
break;
case "Phone":
if (ereg("^((\([0-9]{3}\) ?)|([0-9]{3}-))?[0-9]{3}-[0-9]{4}$",$var)) {
$valid = true;
}
break;
case "HexColor":
if (ereg("^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$",$var)) {
$valid = true;
}
break;
case "User":
if (ereg("^[a-zA-Z0-9_]{3,16}$",$var)) {
$valid = true;
}
break;
}
return $valid;
}
Example:
$email_address = $_POST["email"];
if (isValid("Email",$email_address)) {
echo "Valid Email Address;
} else {
echo "Invalid Email Address;
}
For more information, help, and examples visit RegExLib.com or check out the Mastering Regular Expressions book.
Most Commented