HOWTO: Implement a URL Pattern Interpreter Similar to Django and RoR in PHP (Drop the .php With mod_rewrite)

| 1 TrackBack
mark-kolich-mobi-url-pattern.pngAbout a month ago, I posted a quick update that dealt with an improved REST style URL pattern interpreter for my mobile blog, mark.kolich.mobi.  At the time, the enhancement was to convert the style of my mobile blog URL's from this:

http://mark.kolich.mobi/m.php?a=1

to this:

http://mark.kolich.mobi/m.php/1

This improvement was fairly easy to implement as I described here.  However, I realized today that I wasn't totally satisfied with this enhancement, given that the ".php" in the URL was still visible.  After a little investigation, I discovered that I could use Apache's mod_rewrite to make my mobile URL's look a little cleaner, like so:

http://mark.kolich.mobi/m/1

This URL style is much simpler and cleaner: I dropped the annoying query string, and I also got rid of the .php extension.  Implementing this was actually quite easy using mod_rewrite.  I simply used mod_rewrite to internally re-write any requests starting with "m" to the PHP script that serves up individual articles/posts on my mobile blog.  In my Apache httpd.conf file, I added the following:

RewriteEngine On

RewriteRule ^/m /m.php [L]
RewriteRule ^/twitter /tw.php [L]

Any request starting with "m" will be sent to m.php.  Any request starting with "twitter" will be sent to "tw.php".  Check out my mobile blog to see this re-writing method in action.  The PHP code that parses the REQUEST_URI to determine what article to serve up can be found here.

Simple enough.  Enjoy.

Did You Find this Helpful?

Did you find this post helpful, or at least, interesting?

  

Send Mark a Direct Message

If you'd like to send me a direct message, please do so below. However, I do not publicly post comments or messages submitted directly to me. So, if you're going to try to SPAM me, or my blog, you're pretty much wasting your time.

400 characters remaining

Error

About Mark

A Silicon Valley native, Mark Kolich is a full-time Software Engineer, a casual entrepreneur, and a consultant for hire. A web technologies expert, his current focus is on building powerful and robust cloud-driven web-applications using Java, PHP, Perl, AJAX, DHTML, CSS, and JavaScript. His favorite programming languages are PHP, Java and JavaScript. He uses Linux, enjoys biking to work, loves building great software, and always writes elegant, readable, and maintainable code.

1 TrackBack

Onyx (beta) from Mark S. Kolich on November 6, 2009 12:15 PM

If you follow me on Twitter, you may have noticed I silently launched Onyx a few weeks ago.  In a nutshell, as described on the Onyx homepage ..."Onyx is a social file management tool I built to help me keep... Read More

Twitter (@markkolich)

Translate

About this Entry

This page contains a single entry by Mark Kolich published on August 29, 2009 11:00 PM.

HOWTO: Using Apache's Log4j RollingFileAppender, An Example was the previous entry in this blog.

Use JavaScript and jQuery to Get User Selected Text, and then Do Something (Useful?) With It is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.