Monday, December 22, 2008

mod_rewrite and Apache

Mod_rewrite allows you to rewrite a webpage's url on the fly, and you can rewrite the url to almost anything. It has a lot of uses everything from redirecting multiple WebPages to a new domain without actually changing the title, to making dynamic pages appear static.

However, it is somewhat complicated to learn, and if you make a mistake its also possible to really mess-up your server and create endless loops. Need less to say I don't recommend messing around with this on you live site. The solution, if you want to mess around and experiment with it is, to run a test server on your own computer for test purposes.

My own server ?

Apache by default comes with the mod_rewrite module installed but not enabled. So if you have Apache installed on your home computer chances are that all you have to do is enable mod_rewrite (explained later in this tutorial).

If you don't have Apache installed, you can install it yourself, or get one of the many Apache, php, mysql bundles and install it yourself fairly easily. Most (if not all) of these bundles use the default settings, and mod_rewrite is not enabled by default, so come back and read this tutorial once you are done installing apache web server.

Enable mod_rewrite

1. Find the httpd.conf file (usually you will find it in a folder called conf, config or something along those lines)
2. Inside the httpd.conf file uncomment the line LoadModule rewrite_module modules/ (remove the pound '#' sign from in front of the line)
3. Also find the line ClearModuleList is uncommented then find and make sure that the line AddModule mod_rewrite.c is not commented out.

Check to see if mod_rewrite is enabled

There are several ways to check if its working, I will try to show you the simplest method, Create a .htaccess file (a text file named .htaccess) with the following code in it

Options +FollowSymLinks
RewriteEngine On

Save the file to a subdirectory of your main site something like this

Now attempt to browse to the subdirectory (http://localhost/rewrite-testfolder/). One of two things could happen

- No errors Congrats mod_rewrite engine is now enabled.

- 500, Internal Server Error If you get this message then mod_rewrite was not installed/enabled on your computer.

Tuesday, May 27, 2008

What are Regular Expressions?

Regular expressions are a way of describing a pattern, using this pattern with PHP you can match, examine, replace, and edit strings with extreme versatility and flexibility. This guide covers the basics of Perl Compatible Regular Expressions, or PCRE, and how to use preg_match(), preg_replace(), and preg_split().
Let's dive right into some basic examples, and how to use them.

Pattern Matching
Using preg_match(), we can perform Perl pattern matching on a string. The preg_match() function returns a 1 if a match is found, and 0 if there was no match. Optionally, you can also store the matches in an array, by setting a variable as the third parameter. This can be very helpful for validating data.

$string = "football";
if (preg_match('/foo/', $string)) {
// matched correctly

This would correctly match, because the word football has foo in it. Now let's try a more complicated idea, like validating an email address.

$string = "";
if (preg_match(
$string)) {
// valid email address

This example will validate that an email address is using the correct form. Now lets go into what the various characters to define our pattern do.

Perl compatible regular expressions run the same as Perl for pattern syntax, so we must have a pair of delimiters. We're going to use / as our delimiter.

The ^ at the beginning and $ at the end tells it to look at the start and the end of the string. Without the $ for example, it would still match with more data at the end of the email.

[ and ] are used to define acceptable input, for instance, a-z would allow all lowercase letters, A-Z all uppercase, 0-9 numbers 0 through 9, an underscore, etc.

The { and } define how many characters you are expecting, in this example, {2,4} means each section can be 2-4 characters long, like or .info.

( and ) are used to group sections together, and defines what the string must contain. (a|b|c) would match a or b or c.

A single period (.) will match any characters, in [.] it will match a literal period.

Certain symbols, when you want to use them literally instead of to control your regular expression, will have to be escaped with a (\) These characters are ( ) [ ] . * ? + ^ | $

Pattern Replacing
– preg_replace will allow you to replace anything that your regular expression matches with what you define. A simple example of replacing text is a simple comment remover.

preg_replace('[(/*)+.+(*/)]', '', $val);

This will remove multi-line comments in the form of /* comment */ from CSS and PHP files. The parameters you pass are the regular expression, what you want to replace it with, and the string to use. If you want to use the matches sub patterns you've defined in your regular expressions, $0 is set to the entire match, and $1, $2, and so forth are set to the individual matches for each sub pattern.

Pattern Splitting
preg_split can split a string into pieces by something more complicated than just one or two characters, for example, a way to grab all tags regardless of spacing (though explode() or split() would work better in this situation,) could be...

$tags = preg_split('/[,]/', 'my,tags,unevenly,spaced');