views:

260

answers:

2

Hi

I am building a web application that is client side js heavy with data being pushed in chunks from the server.

I am trying to implement a solution for a friendly URL solution that takes a URL like:

http://exmample.com/find/SomethingHere-or-there

That equates to a series of variables not visible. So it might end up processing as this:

http://exmample.com/index.php?loc=London&type=2&priceCat=10-15

I get the values by passing the friendly URL as a parameter then getting back the full URL.

My initial implementation used a mod_rewrite rule to forward to a script that extracts the friendly url, query the db for a matching friendly url and return the full url with all the params it maps to, build the url string with this then forward to the web address which it will then extract the params to the web app front side.

However I lose the friendly url when I use the php header("Location:http://example.com") function so they see the full URL with params. This is one of the secondary requirements that i am trying to fulfill.

I then thought that as my web app is js heavy, should I try render the page then ajax the params out of the db to my web app? Is this the best way or is there another trick to achieve this that i don't know about?

Not having js enabled client side is not an issue.

attached is some code to outline my inital implementation:

//get the friendly url as a parameter
$goto = explode('=',$_SERVER['QUERY_STRING']);
//$goto[1] = SomethingHere-or-there

//build the friendly URL string
$forwardingURL = 'Location:http://'.$_SERVER['HTTP_HOST'].'/'.getForwardedURL($goto[1]);

//will be http://exmample.com/index.php?loc=London&type=2&priceCat=10-15
header($forwardingURL);

//function that returns the full url param string from the friendly url
function getForwardedURL($friendlyURL){  
$friendlyURL = mysql_escape_string($friendlyURL);
$query = "
SELECT url
FROM FriendlyURLmapping
WHERE friendly_url = \"$friendlyURL\"";

$resultP = mysql_query($query) 

//the full parametised URL will be in $row
$row = mysql_fetch_array($resultP, MYSQL_ASSOC);    
return $row["url"];
}
A: 

Why do you redirect to that parameterized URL? Why don’t you just use that parameterized URL and return the actual contents?

So instead of doing a redirect, do something like this:

$url = 'http://example.com/index.php?loc=London&type=2&priceCat=10-15'; // resolved from http://example.com/find/SomethingHere-or-there
// split URL into its parts
$url = parse_url($url);
// check if requested file exists
if (is_file($_SERVER['DOCUMENT_ROOT'].$url['path'])) {
    // parse and merge URL parameters
    parse_str($url['query'], $params);
    $_GET = array_merge($_GET, $params);
    // include resolved file
    include $_SERVER['DOCUMENT_ROOT'].$url['path'];
} else {
    hedaer($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
}
Gumbo
The goal is to get nice urls.
Dykam
+2  A: 

Normal practice is to map URLs with values in them to the parameterized URL. For example:

http://example.com/London/2/10-15
to
http://example.com/index.php?loc=London&type=2&priceCat=10-15

This can be done like so in .htaccess:

RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /index.php?loc=$1&type=$2&priceCat=$3 [L]

I would avoid redirecting if at all possible. If you want completely different URLs to map to parameters, like your example (/something-here to /index.php?...) then all you need to do is rework your application so that you either pass the parameters to a function that displays the page, or set the variables and include another PHP file that does the processing.

DisgruntledGoat
You could also just use mod-rewrite to export everything after your domain name to the $_URL, then you could just explode it by the slashes and parse each part with PHP.
Breakthrough
Joe
@Joe: where are you storing your parameter mappings, then? The latter part of my answer should help, all you need to do is set `/something-here` to map to `index.php?param=something-here` then use that PHP script to display the necessary information.
DisgruntledGoat
@DisgruntledGoat in the database. When I forward the friendly to my page, I actually use the friendly url as a lookup for the full parameter list/query string to pass to the client side for processing. Will see if i can trim some code down to outline my implmentation
Joe