Nirali Joshi
Nirali Joshi

Reputation: 2008

URL redirection upon parameters

I have a url pattern -- www.domain.info/(unique_number)

for example : http://domain.info/1211.10/09879 where 1211.10/09879 is a unique number

now ,on GET request I want to redirect this url to page.php where page.php will display unique_number's data.

where should i code for getting unique number from url?(i dont want to make directory - 1211.10/09878/)

what is the best way to achieve this ?

Upvotes: 0

Views: 55

Answers (1)

Jean-Karim Bockstael
Jean-Karim Bockstael

Reputation: 1405

To achieve this you must first configure the web server in order to send all requests to the same script, assuming you are using Apache this would be something like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ routing.php [QSA,L]

This will send all requests that don't point to an actual file to the routing.php script.

Now in routing.php the identifier can be accessed through the global $_SERVER['REQUEST_URI'] variable:

// assuming the whole URL is http://domain.info/1211.10/09879?someParameter=someValue
$uri = $_SERVER['REQUEST_URI'];

// remove the leading / and parameters:
$uri = substr($uri, 1);
if (strstr($uri, '?') !== false)
{
    $uri = substr($uri, 0, strpos($uri, '?'));
}

// Here $uri contains "1211.10/09879" and you can carry on

Upvotes: 2

Related Questions