Forum Moderators: coopster

Message Too Old, No Replies

Removing and adding querystrings with simplified urls

         

logoex

10:47 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



I am implementing simplified URL's using apache's rewriteengine.

The Dyanimic URLs look like this:

products.php?brand=24&category=25&theme=4&price=4

The simplified URLs look like this:

products/brand/24/category/25/theme/4/price/4

I need to be able to remove and add brand, theme, category and price.

Below is the current code I use to do this with dynamic url's:


// Add query string to URL
function add_querystring_var($key, $value) {
global $page, $current_url;

if(isset($page)) {
$remove_page_url = preg_replace('/(.*)(\?¦&)page=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
$remove_page_url = substr($remove_page_url, 0, -1);
$url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $remove_page_url . '&');
} else {
$url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
}
$url = substr($url, 0, -1);
if(strpos($url, '?') === false) {
return ($url . '?' . $key . '=' . $value);
} else {
return ($url . '&' . $key . '=' . $value);
}
}

// Remove query string from URL
function remove_querystring_var($key) {
global $page, $current_url;

if(isset($page)) {
$remove_page_url = preg_replace('/(.*)(\?¦&)page=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
$remove_page_url = substr($remove_page_url, 0, -1);
$url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $remove_page_url . '&');
} else {
$url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
}

// $url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
$url = substr($url, 0, -1);
return ($url);
}

I am wondering whether there is a way to convert the simplified url back to the dynamic to run the function, then return a simplified url or if this is the wrong way to go about this?

Thanks! :)

[edited by: jatar_k at 10:49 pm (utc) on Aug. 30, 2007]
[edit reason] disabled smilies [/edit]

logoex

7:30 pm on Aug 31, 2007 (gmt 0)

10+ Year Member



I solved the issue. Here is what I used:

// Add query string to URL
function add_querystring_var($key, $value) {
global $page, $current_url;

if(isset($page)) {
$remove_page_url = preg_replace("/(.*)\/page\/\d{1,5}\/?(.*)/i", "$1$2", $current_url);
// $remove_page_url = preg_replace('/(.*)(\?¦&)page=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
// $remove_page_url = substr($remove_page_url, 0, -1);
//$url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $remove_page_url . '&');
$url = preg_replace("/(.*)\/$key\/\d{1,5}\/?(.*)?\/?/i", "$1$2", $remove_page_url);
} else {
$url = preg_replace("/(.*)\/$key\/\d{1,5}\/?(.*)?\/?/i", "$1$2", $current_url);
// $url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
}

//$url = substr($url, 0, -1);
return $url ."/$key/$value";
}

// Remove query string from URL
function remove_querystring_var($key) {
global $page, $current_url;

if(isset($page)) {
$remove_page_url = preg_replace("/(.*)\/page\/\d{1,5}\/?(.*)/i", "$1$2", $current_url);
// $remove_page_url = preg_replace('/(.*)(\?¦&)page=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
// $remove_page_url = substr($remove_page_url, 0, -1);
//$url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $remove_page_url . '&');
$url = preg_replace("/(.*)\/$key\/\d{1,5}\/?(.*)?\/?/i", "$1$2", $remove_page_url);
} else {
// $url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
$url = preg_replace("/(.*)\/$key\/\d{1,5}\/?(.*)?\/?/i", "$1$2", $current_url);
}

// $url = substr($url, 0, -1);
return ($url);
}

[edited by: eelixduppy at 7:32 pm (utc) on Aug. 31, 2007]
[edit reason] disabled smile faces [/edit]

logoex

8:08 pm on Aug 31, 2007 (gmt 0)

10+ Year Member



One more thing, I realize because search engines have already indexed my x.php?stuff=this&this=stuff format urls I included the old way as well so that it is backwards compatible and works both ways.

// Add query string to URL
function add_querystring_var($key, $value) {
global $page, $current_url;

if(preg_match("/.php/",$current_url)) {
if(isset($page)) {
$remove_page_url = preg_replace('/(.*)(\?¦&)page=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
$remove_page_url = substr($remove_page_url, 0, -1);
$url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $remove_page_url . '&');
} else {
$url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
}
$url = substr($url, 0, -1);
if(strpos($url, '?') === false) {
return ($url . '?' . $key . '=' . $value);
} else {
return ($url . '&' . $key . '=' . $value);
}
} else {
if(isset($page)) {
$remove_page_url = preg_replace("/(.*)\/page\/\d{1,5}\/?(.*)/i", "$1$2", $current_url);
$url = preg_replace("/(.*)\/$key\/\d{1,5}\/?(.*)?\/?/i", "$1$2", $remove_page_url);
} else {
$url = preg_replace("/(.*)\/$key\/\d{1,5}\/?(.*)?\/?/i", "$1$2", $current_url);
}

return $url ."/$key/$value";
}
}

// Remove query string from URL
function remove_querystring_var($key) {
global $page, $current_url;

if(preg_match("/.php/",$current_url)) {
if(isset($page)) {
$remove_page_url = preg_replace('/(.*)(\?¦&)page=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
$remove_page_url = substr($remove_page_url, 0, -1);
$url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $remove_page_url . '&');
} else {
$url = preg_replace('/(.*)(\?¦&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $current_url . '&');
}

$url = substr($url, 0, -1);
return ($url);
} else {
if(isset($page)) {
$remove_page_url = preg_replace("/(.*)\/page\/\d{1,5}\/?(.*)/i", "$1$2", $current_url);
$url = preg_replace("/(.*)\/$key\/\d{1,5}\/?(.*)?\/?/i", "$1$2", $remove_page_url);
} else {
$url = preg_replace("/(.*)\/$key\/\d{1,5}\/?(.*)?\/?/i", "$1$2", $current_url);
}

// $url = substr($url, 0, -1);
return ($url);
}
}

[edited by: coopster at 3:44 pm (utc) on Sep. 5, 2007]
[edit reason] Disable graphic smile faces [/edit]