views:

129

answers:

8

When you edit a question on stackoverflow.com, you will be redirected to a URL like this:

http://stackoverflow.com/posts/1807421/edit

But usually, it should be

http://stackoverflow.com/posts/1807491/edit.php

or

http://stackoverflow.com/posts/edit.php?id=1807491

How was

http://stackoverflow.com/posts/1807421/edit created?

I know that Stackoverflow.com was not created by using PHP, but I am wondering how to achieve this in PHP?

A: 

Using mod_rewrite this can be achieved very easily.

mr-euro
For an up-vote you might take the time to explain *how* this can be achieved very easily using `mod_rewrite`.
David Thomas
I was in a hurry... :-)
mr-euro
A: 

I am poor at this but i do know you can redirect urls using apache mod_rewrite and by touching config files. From what i remember htaccess can be used to redirect. Then internally when the user hits http://stackoverflow.com/posts/1807421/edit it can use your page http://stackoverflow.com/edit.php?p=1807421 instead or whatever you want.

acidzombie24
A: 

You could use htaccess + write an URI parser class.

Ondrej Slinták
+2  A: 

It's called routing. Take a look at tutorials on the subject.

If you use a framework such as cake php it should be built in.

marcgg
+2  A: 

As @mr-euro stated you can use mod_rewrite but front controller is a far better solution. You force every request to index.php and you write your application controlling in index.php.

dfilkovi
Why is it better?
Dominic Rodger
Well this a really a debate question but for me front controller is a far better solution cause it handles ajax, templating and page generation, simply I have a less code, unified structure and simple ajax integration.
dfilkovi
+6  A: 

With apache and PHP, you might perform one of your examples using a mod_rewrite rule in your apache config as follows:

RewriteEngine On
RewriteRule ^/posts/(\d+)/edit /posts/edit.php?id=$1

This looks for URLs of the "clean" form, and then rewrites them so that they are internally redirected to a particular PHP script.

Quite often rules like this are used to route all requests into a common controller script, which might do something like instantiate a "PostsController" class and ask it to handle an edit request. This is a common feature of most PHP application frameworks.

Paul Dixon
+1  A: 

It's indeed done by mod_rewrite, or with multiviews. But i prefer mod_rewrite.

First: you create a .htaccessfile with these contents:

RewriteEngine On
RewriteRule ^posts/([0-9])/(edit|delete)$ /index.php?page=posts&postId=$1&action=$2

Obvious, mod_rewrite must be enabled by your hostingprovider ;)

Ben Fransen
+2  A: 

You use Apache's .htaccess/mod_rewrite, and optionally a PHP file, which is the approach I like to take myself.

For the .htaccess, something like this:

RewriteEngine On
RewriteRule ^(.*)$ index.php

Then in your PHP file, you can do something like this:

The following should get everything after the first slash.

$url = $_SERVER['REQUEST_URI'];

You can then use explode to turn it into an array.

$split = explode('/', $url);

Now you can use the array to determine what to load:

if ($split[1] == 'home')
{
// display homepage
}

The array is starting from 1 since 0 will usually be empty.

Chris Clarke