Let me explain, this isn’t a apache question really (or at least I don’t think so). I’ve noticed some sites that run on PHP but I notice they link to www.example.com/example instead of /example.php
So I checked if they were doing it through apache, so I did www.example.com/example.php and a 404 error came up.
Now I’ve tried using apache to remove the extension and was successful, but I noticed I was able to access the site as example.com/example.php and example.com/example. If I accessed the site with the extension and all links refenced without the extension, the links would break.
A good example would be www.last.fm. On the index page everything is last.fm/?variable=foo. Other pages such as Music is last.fm/music. It looks like those types of pages have some mod rewrite as certain links have last.fm/music/reference/to/somewhere.
But were you to access last.fm/index.php?variable=foo, it would return a 404, likewise last.fm/music.php.
So I’m wondering what methods are used to accomplish this, if anyone knows where I can read more about it.
It’s mod_rewrite still. You can rewrite any URL to any file, it doesn’t have to be “example to example.php”. When you type in /music/reference/to/somewhere, that might be handled by secret.php.
More likely with big sites, all requests are being directed to a single .php file regardless of the URL, and that single .php file has all the logic in it for what to do with all the different URLs. If you want to see an example you can try yourself, download WordPress. All URLs are rewritten to index.php.
I’m wondering if there would be a delay in response time(if a heavy site). I’m assuming they are doing it with one big switch statement and include certain pages based on the parameter passed.
Btw, now that I look at sitepoint.com they seem to do the same(not the forums but every other page)?
Not likely, more sophisticated systems will automate the task by mapping a combination of the url pieces to an individual object which is forwarded the request and implements it.
Alright, so I’ve been able to implement this into a test project to see how it all works.
It seems rather easy once you have it going. One question does come to me now though. I’m using $_SERVER[‘REQUEST_URI’] to break down the URL by forward slashes. The question I have is that I can no longer use GET variables as they are treated as a whole.
I’m trying to figure out a way on how to keep using them, example: