Looking for examples on how to remove extension

Hey,

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.

Thanks

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.

That’s very interesting way of doing things.

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)?

The overhead is very minimal. If you get to that point where stuff like this matters, you have far more rewarding things to optimize.

A big switch would be hard to maintain, but conceptually, yes, branching is used.

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:


$arrPage = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
$strRef = $arrPage[0];
$strView = $arrPage[1];

If I do:

www.example.com/?ref=home OR www.example.com/example/?ref=foo

Will be treated as a full string and not as a GET variable. Does anyone know the workaround for this?

If you run from a php script, you will have displayed all sorts of informaton about your server.

A part of that information is $_SERVER variables, where you will be able to find the alternative that you’re after.

It might be SCRIPT_FILENAME or SCRIPT_NAME, but you may want to confirm that for yourself by comparing with phpinfo()