Categories
Uncategorized

Tidying up your urls

By no means am I the first to write about this, but, hey – it’s my blog, and I can write what I want, right?

h3. What I’m at today

I set about rewriting my very first commercial site; four whole years ago… And while looking at it now with an eye for standards and all that jazz I really don’t like it. But then, I didn’t like the first three designs for this site either… Have a look at the site, here.

I’m doing the site in PHP, not because I want to, but because I know it better than Rails and I really want to get my stack of projects cleared and get on with things…

So… I wanted to help along the SEO for the site, to give it user and indeed machine-friendly URLs. As far as I know there are a few ways to do this. Through .htaccess or through the server-side language of choice. I chose .htaccess because it provides a central point of reference; means I don’t have to write “preg_replace” six hundred times; and so on.

h3. Here’s what I want:

My site sections will be as follows.
* home
* interior
* bedrooms
* gardens
* location
* reservations
* contact

My untidy nasty url looks like this:

/index.php?section=4&page=3*

What I want it to look like is this:

/gardens/3/

h3. Here’s what I’ll do:

The RewriteRule and a bit of ^, & and $.

Ensure that…

RewriteEngine on

And then…

RewriteRule ^gardens/([0-9])$ gardens/$1/
RewriteRule ^gardens/([0-9])/$ index.php?section=4&page=$1

RewriteRule is the command which takes the first string and processes it into the second. ^ and $ delimit the first string and the remainder is the second. [0-9] means that you have a number. In the same way [a-z] would be a smallcase letter and [A-Z] would be a capital.

h3. A quick lesson in Reg Exps

You might want to ask some people who know better… but here goes.

Through being ‘required’ to use stuff like Vi at work I’ve had to learn some regexps to make life more managable!
* [0-9] Number
* [a-z] Letter (smallcase)
* [A-Z] Letter (CAPS)
* [a-Z] Letter (any)
* [0-9]+ Series of numbers (+ is applicable to a,z etc as well as 0-9s.)

Just in case you missed it: to have gardens/536 (If I wanted 536 pages of garden content…) then I would need to replace
^gardens/([0-9])$ with ^gardens/([0-9]+)$
This will of course allow gardens/1231231 if some viewer wants to be smart, but that’s what error handling’s for. Isn’t it?