What is the best way to implement a friendly URL with multiple variables using mod_rewrite?

I am creating a web application that is js client-side heavy, data is being sent to chunks from the server.

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

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

This means that a number of variables are not displayed. Thus, it may end up processing as follows:

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

I get the values ​​by passing a friendly URL as a parameter, and then return the full URL.

My initial implementation used the mod_rewrite rule to forward to a script that retrieves the friendly url, requests db for the corresponding friendly url and returns the full url with all the parameters it maps to, builds a url string with that then go to the web address, which then fetches Options on the front side of the web application.

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

Then I thought that since my js web application is heavy, should I try to display the page and then add the parameters from db to my web application? Is this the best way or is there another trick to achieve this that I don't know about?

The use of the js client side is not allowed.

some code is attached to describe my internal 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"]; } 
+4
source share
2 answers

It is common practice to map URLs with values ​​in them to a parameterized URL. For instance:

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

This can be done like this: .htaccess:

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

I would avoid redirecting, if at all possible. If you want different URLs to be displayed on different parameters, for example your example (from /something-here to /index.php?... ), all you need to do is remake the application so that you either pass parameters functions that display the page, or set variables and include another PHP file that does the processing.

+3
source

Why are you redirecting to this parameterized URL? Why don't you use this parameterized URL and return the actual content?

So instead of doing the redirection, 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'); } 
0
source

All Articles