Jump to content

[SOLVED] QUERY_STRING mod_rewrite problem


erme

Recommended Posts

Hi, I have the following code (not written by me)

 

if($_GET)
        {
            $args = explode("&",$_SERVER['QUERY_STRING']);
            foreach($args as $arg)
            {
                $keyval = explode("=",$arg);
                if($keyval[0] != "offset" And $keyval[0] != "ipp") $this->querystring .= "&" . $arg;
            }
        } 

 

and have this in the htaccess:

 

RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?offset=$1&ipp=$2&county=$3& [L,NC]

 

to rewrite the horrible URL. All works great, however when ever I run the script it outputs &county= in the URL, where as I want it to output just the county name.

 

In otherwords, I want:

http://www.site.com/index/5/10/devon/

 

and not:

http://www.site.com/index/5/10&county=devon/

 

Sorry if I'm not explaining myself very well. Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/168964-solved-query_string-mod_rewrite-problem/
Share on other sites

Hi Bricktop,

 

Unfortunately the problem persists. I don't think its an issue with the htaccess, but rather the PHP. I'm very new to PHP and, as I said, didn't write the code. The code is from a paginator and is where the problem happens when flicking between pages.

Example:

 

http://s239382210.websitehome.co.uk/

 

First select a county eg devon then a page number at the bottom. Notice what happens to the url.

 

http://s239382210.websitehome.co.uk/index/2/10&county=devon/

 

If you change &county=devon to /devon it still works.

Hi erme,

 

I think it's because the URL's from the menu are different to those of the Pagination.

 

For example, if I click "devon" from the menu at the top the URL is:

 

http://s239382210.websitehome.co.uk/index/devon

 

However, now I'm on the "devon" page, if I click the pagination menu to go to page 2 I get the URL of:

 

http://s239382210.websitehome.co.uk/index/2/10&county=devon/

 

I would expect a URL of:

 

http://s239382210.websitehome.co.uk/index/devon/2/10

 

Otherwise, can you add the following to your code (inside the foreach loop):

 

echo $_SERVER['QUERY_STRING'];
echo $keyval;

 

That way we can see what the PHP is outputting to the browser.

OK, $arg is also outputting county=devon.

 

This is where the problem lies, but are you able to post more of the pagination code?  Would be nice to see the code which actually generates the links.

 

Otherwise, try changing your code to:

 

if($_GET)
        {
            $args = explode("&",$_SERVER['QUERY_STRING']);
            foreach($args as $arg)
            {
                $keyval = explode("=",$arg);
$arg = strrpos($arg,'=');
$arg = substr($arg,$i+1);
                if($keyval[0] != "offset" And $keyval[0] != "ipp") $this->querystring .= $arg;
            }
        } 

 

Here I'm taking the $arg variable, and outputting just the bit after the = using the $arg = strrpos($arg,'='); and

$arg = substr($arg,$i+1); lines of code.

 

Make sure you take a backup before doing this but without seeing the rest of the code hard to see exactly where this is fallingover.

 

Thanks

underneath $keyval = explode("=",$arg);

 

paste this

echo "_SERVER['QUERY_STRING']: <br /><br />".nl2br(str_replace(' ',' ',print_r($_SERVER['QUERY_STRING'],true)))"<br /><br />";

echo "Arg: <br /><br />".nl2br(str_replace(' ',' ',print_r($arg,true)))"<br /><br />";

echo "Keyval: <br /><br />".nl2br(str_replace(' ',' ',print_r($keyval,true)))"<br /><br />";

 

so we can see the contents of $keyval and the others...

 

 

 

(lol i was being a retard :P)

 

oops... put dots between )"

 

echo "_SERVER['QUERY_STRING']: <br /><br />".nl2br(str_replace(' ',' ',print_r($_SERVER['QUERY_STRING'],true)))."<br /><br />";

echo "Arg: <br /><br />".nl2br(str_replace(' ',' ',print_r($arg,true)))."<br /><br />";

echo "Keyval: <br /><br />".nl2br(str_replace(' ',' ',print_r($keyval,true)))."<br /><br />";

 

----------

 

_SERVER['QUERY_STRING']:

offset=4&ipp=10&county=dorset

Arg:

offset=4

Arg:

ipp=10

Arg:

county=dorset

Keyval:

Array
(
    [0] => county
    [1] => dorset
)


String:

&county=dorset

 

I got my script to output that, so it's giving the right value..

 

post the querystring function

I did this:

<?php
echo "<b>_SERVER['QUERY_STRING']: </b><br /><br />".nl2br(str_replace(' ',' ',print_r($_SERVER['QUERY_STRING'],true)))."<br /><br />";

$args = explode("&",$_SERVER['QUERY_STRING']);
foreach($args as $arg)
{
$keyval = explode("=",$arg);
echo "<b>Arg: </b><br /><br />".nl2br(str_replace(' ',' ',print_r($arg,true)))."<br /><br />";
if($keyval[0] != "offset" And $keyval[0] != "ipp") $thisquerystring .= "&" . $arg;
}



echo "<b>Keyval: </b><br /><br />".nl2br(str_replace(' ',' ',print_r($keyval,true)))."<br /><br />";
echo "<b>Thisquerystring: </b><br /><br />".nl2br(str_replace(' ',' ',print_r($thisquerystring,true)))."<br /><br />";

 

so the problem probably lies with the querystring function

actually no, I'm thinking it's to do with yeah the mod_rewrite

 

because it's adding the &country=devon to the URL

 

but it's just not rewriting it

 

just did some research

 

http://www.daniweb.com/forums/thread7826.html#

 

try this as your mod_rewrite rule:

 

^index/([^/]+)/?([^/]+)/?([^/]+)/?$

if ($this->showAll == true) $this->return .= ($_GET['offset'] == 'All') ? "<a class=\"current\" style=\"margin-left:10px\" href=\"#\">All</a> \n":"<a class=\"paginate\" style=\"margin-left:10px\" href=\"$WEBSITE/1/All$this->querystring/\">All</a> \n";

 

needs to be changed

 

 

you need to strip out the .php from $WEBSITE

 

 

 

//edit

 

actually I'm not quite sure where it's coming from :S

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.