mod_rewrite semi tutorial

Please note!! I have shamelessly stolen this article from my old and (even if I do say so myself) rather boring blog. So please ignore the terrible writing style! I’ll rewrite this article in the future, but until then, I present you with version 1.0 There are a lot of tutorials out there regarding apache’s [...]

By Clinton Montague

Please note!! I have shamelessly stolen this article from my old and (even if I do say so myself) rather boring blog. So please ignore the terrible writing style! I’ll rewrite this article in the future, but until then, I present you with version 1.0

There are a lot of tutorials out there regarding apache’s very powerful mod_rewrite module. They always seem to offer every solution except the one which you want! Here is an example of a mod_rewrite script which I commonly use.

1
2
3
4
5
6
7
8
9
10
RewriteEngine on
 
# Directories which we allow the browser to see
RewriteCond %{REQUEST_URI} !^/css
RewriteCond %{REQUEST_URI} !^/js
RewriteCond %{REQUEST_URI} !^/uploads
 
# Redirect all other 'directories' to the main php page
# for it to work out what to do.
RewriteRule ^(.*)$  index.php?/$1 [L]

There are a few directories which I WANT the browser to be able to access, the images folder for example. There are a lot of directories I DON’T want the browser to be able to access, and this is where my solution comes into play.

A quick code run-through:

  • Line 1 turns the module on
  • Lines 4-6 are the directories which the user is allowed to access. In pseudo-code it would look something like this:
    IF (the url doesn’t start with /css AND the url doesn’t start with /js AND the url doesn’t start with /uploads)
    THEN do the url rewriting.The scary looking %{REQUEST_URI} is just an apache environment variable.
  • Line 10 is the ‘interesting’ bit. The ^ means that the pattern starts at the beginning of the url, the $ means that the pattern finishes at the end of the url, and the .* means match any charactor any number of times. Because the .* is in parentheses, it is then stored in the variable $1. The next bit is easy, just tell apache where you want to redirect to, in this case /index.php?/whatever/the/url/was.

Another great thing about doing it this way is that it’s not easy to get a 404 error, apache just re-writes the url and passes it to index.php, which we know exists so if the worst comes to the worst, it can always just show the home page.

If you need a bit of bed-time reading, I recommend the apache documentation on mod_rewrite. There is a GREAT cheat sheet over at ilovejackdaniels.com (there are also some for PHP, Javascript and the like. Much recommended).

Tags: , , ,

Me on the intertubes

It wouldn't be right to have a web site, be a nerd, and not take this opportunity for a bit of shameless pluggery.

If you like all these weird and magic internet page things, you should try Coaster Nerd, Oxford Bloggers or, if you'd really like some unexplainable sourcery, is it the weekend yet.

Also you should follow me on Twitter ;)

Where to find me

I can be found fairly regularly at Oxford Geek Nights, OxTuttle and other general nerding out activities in and around Oxford.

I am also often found in my natural habitat which is anywhere that you can find an adreneline machine. I frequent English theme parks more often than is healthy, and take trips abroad to sample foreign rides.

From the aviary

Was having a decidedly crap day of development. Just got much better. It's nice when things go your way. #win 1 day ago

Subscribe

Slightlymore Posts RSS feed

Comedy disclaimer

These posts were written at the time of writing and may not still be accurate or reflect my opinions any more. In fact, they may not even be correct or representative at the time of writing. You see, when I write, I just write. I don't do that thing which they always told you to do at school and plan what you want to write first. Because of this, there will almost certainly be a load of typos, grammar errors and incorrect facts and references. If this makes you feel queasy, I apologise that you had to get to the bottom of the page to read this, and hope that some day, you'll learn to forgive me.

What's this site?

Slightlymore started off all too formally for my liking. In the depths of the archives, you'll find tutorials. However if you read the more recent posts, you'll find them a lot more playful and about fun and interesting stuff.

Annoyingly difficult to use tag cloud