blog

Advertising, marketing, mac stuff and bla.st news from bla.st. Feel free to get in touch


Recent Entries

WordJot has been launched
27th March 2008

Bla.st is for sale
2nd March 2008

Entrecard - "Looks a lot like bla.st"...
1st February 2008

bla.st downtime
15th November 2007

New card feature: Headings! Some simple SEO tips
22nd October 2007

Make money online with the bla.st referral link
5th October 2007

Several subtle bla.st changes
27th September 2007

5 Reasons BlogRush Has Already Failed and Why Bla.st Will Take Over
24th September 2007

Older Entries

bla.st should do this...

More great bla.st reviews

New Bla.st Marketplace Page: See who's using the bla.st widget

How to Never Clear Out Your Inbox Again

When advertising attacks

A month with bla.st review

More sites using the bla.st widget

bla.st vs milliondollarhomepage & pixelotto

Widget referrals now earn 50%

6 months of website promotion tips in 2 minutes

Make money with bla.st cards on your site: the bla.st widget and referral programme

1000 cards!

bla.st faster and more reliable thanks to new server

bla.st outage

bla.st RSS feeds now enabled and more

Internet Advertising on the Rise

Keep Safari and OSX running fast: Work around the memory leaks.

How to stop email spam, worldwide, for good.

Update your card image and other admin improvements

New Stuff Including a New Header Design, Section Tabs and a Redesigned Footer

Great list of Interesting Marketing Sites (including this one!)

Our lesson of the day: expectations

jobfavors.com and woorkboot.co.nz improvements

johnchow.com reviews bla.st through reviewme.com

Which is better for your site: www or no www?

New Feature: Info hover panel now with Language, Card Type, and Caption

Link previews (like Snap) are completely backwards

Super friendly URLs - handling spaces with URL Rewrites and PHP

bla.st news - no more countries!

the old million dollar hompage passes pixelotto.com in traffic

The 3 best minimal blogs on the web

What print designers should know about fonts on the web

How to stop RSS feeds from ruining your productivity (without giving them up)

bla.st interview on profy.com

OmniWeb vs Safari/Saft - which is the most awesome?

MacProMozaik - A bla.st inspired site launches :)

New Feature - "Popular Cards" shows the most and least popular cards on bla.st

PageBull, an interesting visual search engine

11 tips for growing large scale websites from nothing

blatant bla.st bribery - free card upgrades for blogging about/linking to bla.st

Testing out memcached (a technical announcement)

The Ad Generator

"bla.st partners with adetchr.com" or "laser etched macbook pros are so cool"

Kind words from WinExtra.com

Has Pixelotto stalled?

A bla.st milestone: Over 2 million cards served!

Featured Card: Blund

New bla.st record set thanks to Spanish blogs

The hypocrisy of the crippled Apple iPhone

The place where spam is welcome: bla.st

Free advertising that's actually usefull

14 big Apple iPhone questions

Someone wants to make their own bla.st

iChat update just before Macworld... coincidence?

Macworld bla.st promotion: Free bla.st card design for up to 25 Mac related companies

Last Minute Prediction: The Apple WiFi Phone

New features: "Small" cards, card expiration notifcations and extended time online

9 essential things everyone should know about email

The Genius of the Visually Inconsistant Mac User Interface, Part 2

Process your URLs in PHP with Apache mod_rewrite and wildcard DNS

16 simple tips for making your site search engine friendly

Information Architects predict "More money for Internet Advertising" in 2007

Photoshop CS3 beta mini review and performance tests on a G4 PowerBook

The Genius of Apple's User Interface Themes

stuff.co.nz and nzherald.co.nz relaunch: uncanny similarities

Advertising Made Awesome - bla.st has a new colour scheme

New tiny cards!

adgridwork - A free advertising network to try

Site downtime and DNS issues

Merry Mailer Madness Month

Ideas for a website tag system

New front page design

bla.st iPod competition winners announced

Turkish cards coming to bla.st

Improved outgoing links and stats

Competition finished

The Obfuscator - Protect emails from spam

New Ajax style 'related keywords' and 'filter by keyword' buttons

How OS X menus could be made more useful

Only 7 more days to go in the draw to win one of 2 iPods

6 Sites That Have Evolved From Pixel Advertising

43ads - another web advertising money making scheme

Funniest ad for shoes ever

How web browser uploads could be made awesome

Win an iPod competition launched

The Dominion Post writes about bla.st

6 ways to improve the million dollar homepage and make your fortune

Found a similar money making scheme to bla.st!

bla.st linked up to Google Maps

bla.st online card builder now online

blastastic

« Blog

Process your URLs in PHP with Apache mod_rewrite and wildcard DNS

30th December 2006 02:30AM

Every cool web 2.0 application these days features nice URLs, where instead of long complicated query strings eg:
http://bla.st/?section=design&page=1
we have a nice, simple folder structure:
http://bla.st/design/1/

We use Apache's mod_rewrite to accomplish this. mod_rewrite is very powerful, but can be tricky to debug. For our bla.st project, we decided to put all URLs through a single PHP script, and process them there. The advantage is then we can do what ever we want with the URLs.

Let's go over the URLs that we wanted working for bla.st. First of all we wanted to divide bla.st up by country using sub domains eg. http://nz.bla.st/ for the New Zealand site, http://us.bla.st/ for the US site. Our first step was to set the DNS A records for bla.st and *.bla.st to our server IP address. The * is a wildcard, so anything.bla.st (including www.bla.st) will take you to our server.

In our Apache httpd.conf file we set the ServerName and ServerAlias for our bla.st virtual host as follows:


ServerName bla.st
ServerAlias *.bla.st


This means that http://anything.could.go.here.bla.st/index.php would go to the file index.php

The next step is to set a rewrite rule that puts any URL through a single controller file, in this case we choose index.php. Here's our completed virtual host entry, with the reason for each line in a comment under the line.


<VirtualHost [OUR IP]>
RewriteEngine on

RewriteRule ^/.*(\.css)|(\.php)|(\.jpg)|(\.png)|(\.html)|(\.gif)|(\.js)|(\.txt)|(\.ico)$ - [L]
# [L] stops mod_rewrite processing so any URLs ending with those filetypes will be treated like normal.

RewriteRule ^/static/(.*) - [L]
# stops mod_rewrite processing the URL for anything in the folder 'static'

RewriteRule ^/([a-zA-Z0-9_/+.!*',()~-]+)$ /index.php [QSA]
# pass any request for a folder to index.php. [QSA] ensures any query string is passed on

ServerName bla.st
ServerAlias *.bla.st
</VirtualHost>


The result is any request for the following style URLs, will go through index.php:
http://nz.bla.st/my/folder/
http://nz.bla.st/web-2.0/
http://bla.st/login/
http://www.bla.st/design/2/?view=big

In index.php we have code that splits the URL and directory into arrays:


<?php

// get the server variable that contains the directory structure eg. /design/2/, and make sure it's lowercase.
$string = strtolower($_SERVER['SCRIPT_NAME']);

// make sure there are no funny characters our app doesn't allow
$string = ereg_replace("[^a-z0-9_/+.!*',()~-]", "", $string);

// explode the URL around the /
$uri_array=explode('/', $string);


// make sure a request for /index.php is treated the same as a request for /
if ($uri[1]=="index.php") $uri[1]="";

// explode the HTTP_HOST around the . so www.bla.st will become an array of www, bla and st
$host_array=explode('.', strtolower($_SERVER['HTTP_HOST']));

// if the first item is 2 characters, treat it as a country code, otherwise
if (strlen($_HOST[0])==2) {
$country_code=$_HOST[0];
}

// continue with our app

?>


Now we have the URL split up nicely, we can include files as needed depending on the URLs requested. There is a slight overhead having to go through the index file, but the flexibility might be worthwhile for your project.

Digg!
Leave comments at Digg



Got a website? Promote it on bla.st free