Skip to content

RegEx and the Mod_rewrite reference sheet


Dave Child, a Brighton UK based Web developer offers a series of free Reference/Cheat sheets that I think most web developers will find of value. He obviously invested a lot of time and effort into them and it shows. He's created free cheat sheets for MySQL, Javascript, PHP, RGB colors and the subject of today's Gizmola.com entry, Apache mod_rewrite. If you've used Apache there's a good chance you've heard of mod_rewrite, although you might not understand what it can do for you. Lately, a lot of PHP scripts are including functionality that will create mod_rewrite rules and add them to a .htaccess file to support "SEO friendly" url's. Serendipity, which runs my blog, and the popular PHP Content Management System Mambo/Joomla are just two examples of packages that can do this for you.

For example, if your script uses a parameter like this: http://www.yoursite.com/home.php?id=5

A mod rewrite rule can allow users to use: http://www.yoursite.com/home/5/

Many search engines seem to penalize sites that use URL parameters, and are more likely to index and value the non-parameter version. Of course, that's not all that mod_rewrite can do. Many people use it for its ability to offer at least a layer of Hotlink protection, or to handle things like forwarding visitors from obsolete pages that might exist in a search engine to the new ones.

Using and understanding the black magic of mod_rewrite requires a basic understanding of Regular Expressions or as they are typically known: RegEx. If you're new to RegEx, think of them as wildcards on steroids. The October Lampsig meeting will present RegEx basics, and if you're in Los Angeles, definately try and attend the presentation, which will be given by Peter Benjamin. Check out Dave Child's succint reference for this powerful module whether or not you plan to attend Pete's presentation.

One of the biggest problems I found in learning to use RegEx is in the area of debugging and testing. The RegEx Coach is a great tool for this, and runs under Linux or Windows. I should mention that this is "donationware" and the author requests a donation if you find it of long term use.

If you're a book type of person, then Mastering Regular Expressions is pretty much considered the Bible on the subject. When the first edition was released, it was far ahead of the curve, but the web has caught up and now there are a lot of very good tutorials and resources available online. In particular Regular-Expressions.info is a really well done site that covers RegEx in a substantial way. Don't get me wrong, Mastering Regular Expressions, especially Edition II (yes I have both editions) is a great book, but can be intimidating. You can pick up the basics of RegEx in one of the numerous online tutorials available. Get the book if you want to really dig into the subject matter in more detail.

RegEx libraries do tend to differ, given the language, tool or editor you're using, but the basic principles are the same. Just about any language going has one or more ReGex libraries available, so your investment in learning them will pay off many times over, once you begin to understand their power.

Defined tags for this entry: , , ,

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

Regex Basics on :

*Thanks for the regex references. I've seen a few parsers online for testing out regular expressions, but have never played with RegEx Coach.

I use ISAPI Rewrite extensively (it's an IIS utility that's about 98% compatible w/ Mod_Rewrite). So I've had to get up to speed w/ RegEx. But I still run into situations that leave me scratching my head.

Ciao!
Canonical

Add Comment

Pavatar, Gravatar, Favatar, MyBlogLog, Pavatar author images supported.
BBCode format allowed
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
Form options