I’ve been building a database of bars and nightclubs in my home city of Portland.
However, I didn’t like the messy URLs generated when I passed the “bar_id” parameter to the PHP script:
<a href=bar_info.php?bar_id=1> Shanghai Tunnel </a>
I wanted a nice clean way to make the URL contain both the bar’s name, extract information based on a value passed to the database, AND appear to both search engines and visitors as a static HTML page.
First I added a new column to my mysql table called “static_url”. Then I wrote a function that generated a 2nd version of the bar’s name with all odd characters stripped out, while spaces where replaced with a hyphen (“-“). Then this info was added to the static_url column of my table for the corresponding bar.
For instance, the record row for “Shanghai Tunnel” now contains a value of “shanghai-tunnel” for the “static_url” column
Unless there are two Shanghai Tunnels in Portland, this is unique a non-numeric indentifier for each bar. So instead of accessing bar information by passing a numeric ID value, I could now pass “shanghai-tunnel” as a value.
I can extract the same information about Shanghai Tunnel using “bar_info.php?static_url=shanghai-tunnel” as I could with “bar_info.php?bar_id=1”.
So my next step is to make bar_info.php?static_url=shanghai-tunnel look like a static html page. So I
- Point it to a non-existant folder called “bar”
- Point it to a non-existant file called “shanghai-tunnel.html” located in the non-existant folder called “bar”
This is what my URL now looks like
<a href=http://www.pdxbars.com/bar/shanghai-tunnel.html> Shanghai Tunnel </a>
Now it’s time to make these non-existant files appear real. Here comes the magic of mod_rewrite and regular expressions (which is something I’m still learning about). Basically, we want to tell the URL “bar/some-bar.html” to be processed as “bar_info.php?static_url=some-bar”
Open up your .htaccess file (make sure you have a back up in case you screw something up)
RewriteRule ^bar/([^.]+).*$ bar_info.php?static_url=$1
Make sure your .htaccess is in your root web folder, and off you go. Modify the script at will, this should work with any sort of web programming language such as Perl, ASP, ColdFusion, JSP, or whatever, provided you are running apache with mod_rewrite enabled.
*For those uptight programmers out there who don’t like the idea of using non-numeric identifiers. My decision was based on the fact that
tells more about the page to both users and search engines than