Page 1 of 1
clean and rearange query string
Posted: Mon Feb 05, 2007 5:11 am
by louie35
I need some help with this.
First - rearange the query string
eg. page.php?mid=123&pid=123
to
eg. page.php?pid=123&mid=123
:::::::::::
Second - I want to clean the query string from duplicate values
eg. page.php?pid=123&mid=123&pid=144
the bold part is the duplicate no matter what value it has i want to keep the first one
eg. eg. page.php?pid=123&mid=123
Posted: Mon Feb 05, 2007 6:03 am
by CoderGoblin
Couple of questions....
1) Where are these links being created ?
2) Why do they need to be tidied ?
It would make a lot of sense to correct the creation of the links/passthoughs rather than correct them after they are sent. You could do things with $_SERVER['REDIRECT_URL'] but I would try to avoid it if possible. Make the original links correct first.
If you have page.php?pid=123&mid=123&pid=144 for some reason and you have pid=123 as the main thing, you may wish to save the value in a session but without having any idea on the workflow/code it is hard to be more precise.
Posted: Mon Feb 05, 2007 6:17 am
by louie35
I am triyng to use mode-rewrite and shorten the links form
page.php?pid=123&mid=123&pname=product_name&mname=cat_name
to
page/123/123/product_name/cat_name
the reason i need to rearange them.
I have a function to modifies the links created as the website wasn't design with mode-rewrite in mind from start but all links are parsed by this funtion
Code: Select all
function link_url($page){
global $km,$kc,$kb,$ks,$p_id;
$url_link = $page."?";
$url_link = str_replace(",","_",$url_link);
$url_link = str_replace("-","_",$url_link);
$url_link = str_replace(">","_",$url_link);
$url_link = str_replace("/","_",$url_link);
$url_link = str_replace("(","_",$url_link);
$url_link = str_replace(")","_",$url_link);
$url_link = str_replace(" ","_",$url_link);
$url_link = str_replace("~","_",$url_link);
$url_link = str_replace('__','_',$url_link);
$url_link = str_replace('__','_',$url_link);
$url_link = str_replace('__','_',$url_link);
$url_link = str_replace('__','_',$url_link);
$url_link = str_replace("??","?",$url_link);
$url_link = rtrim($url_link,"?");//change the / to ?
return $url_link;
}
i could easily rearange them inside the function using if..then statements but how about removing duplicates just in case.
Posted: Mon Feb 05, 2007 6:27 am
by CoderGoblin
Eek... Mod rewrite...

. sorry only know the basics for that...
I would still try to change the function to get the URL correct. You may want to use preg_match_all to get all the options, save them in array and do an array_unique or something similar.
If this is legacy code you have to ask yourself, how much time would it take to get the code working properly rather than "hack it" to get it working with mod rewrite (if it can be done).
Posted: Mon Feb 05, 2007 7:42 am
by m3mn0n
mod_rewrite is definitely a good solution for you.
You really don't have to change much in terms of code. All you need to do is make sure your links are updated and presto, the rest is done for you if you apply the right rewrite rule(s)
We have a forum that covers that topic:
viewforum.php?f=31
You'll find many good examples there. Plus there is always Google if you're looking for tutorials.
If you get stuck, feel free to post a question. I know there is many people highly experienced with mod_rewrite in this community (me being one) and we'll be happy to help you solve the problem.
Posted: Mon Feb 05, 2007 7:49 am
by louie35
thanks.
I am working at the moment on re-aranging the querystring after which I'll be loking to get the right mod-rewrite in place using .htaccess
The website has too many pages to edit the links manually but as I said all links are parsed by the function above, which I am working on adding more features to create them the right way.
Hving problems with duplicates but I'll get arround that (hope)