SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination Error with URL rewrite change

    I'm trying to tweak some code that broke because of an SEO update - the site now uses a mod rewrite in apache to change the URL to a more SEO friendly one. The problem is that the code below returns the "index.php" page and not the SEO friendly one, so the pagination doesn't work.

    When I change $link = $_SERVER['PHP_SELF'] to $link = $_SERVER['REQUEST_URI'] - the code works, but doesn't strip out the query string.

    More coffee hasn't helped in 2 days. Anyone?

    Code:
    function printResultSetLinks($numResults, $p_results_per_screen = RESULTS_PER_SCREEN) {
    	$screen = (!empty($_REQUEST[SEARCH_GROUP]) ? $_REQUEST[SEARCH_GROUP] : 1);
    	$queryString = "?" . $_SERVER['QUERY_STRING'];	
    	// Remove the SEARCH_GROUP param, if it was present.
    	// this allows easier insertion of another SEARCH_GROUP param.
    	$searchGroup = isset($_REQUEST[SEARCH_GROUP]) ? $_REQUEST[SEARCH_GROUP] : "";	
    	if (strpos($queryString, "&" . SEARCH_GROUP . "=" . $searchGroup) >= 0)
    		$queryString = str_replace("&" . SEARCH_GROUP . "=" . $searchGroup, "", $queryString);
    
    	print("<p align=center><SMALL>");
    	// Again this means we are only displaying results in batches.
    	// Here we give links to all batches.
    	$firstBatch = ($screen - RESULT_RADIUS < 1) ? 1 : $screen - RESULT_RADIUS;
    	$lastPossibleBatch = floor(($numResults - 1) / $p_results_per_screen) + 1;
    	$lastBatch = ($firstBatch + (RESULT_RADIUS * 2 - 1) > $lastPossibleBatch) ? $lastPossibleBatch : $firstBatch + (RESULT_RADIUS * 2 - 1);
    			
    	// Print out the "Prev" link.
    	if ($screen > 1) {	
    		$newQueryString = "$queryString&amp;" . SEARCH_GROUP . "=" . ($screen - 1);
    		$link = $_SERVER['PHP_SELF'] . $newQueryString;
    		$output = "<a href=\"" . $link . "\">Previous</a>";
    		print($output . "&nbsp;\n");		
    	}
    
    	// Print the numbers that represent screen batches. 
    	for ($i=$firstBatch; $i<=($lastBatch > $firstBatch ? $lastBatch : -1); $i++) {
    		if ($i > 1)
    			print "|&nbsp;&nbsp;"; 	
    		$newQueryString = $queryString . "&amp;" . SEARCH_GROUP . "=" . $i;
    		$link = $_SERVER['PHP_SELF'] . $newQueryString;
    		$output = ($i != $screen) ? "<a href='$link'>$i</a>" : "$i";
    		print($output . "&nbsp;\n");
    	}
    
    	// Print out the "Next" link.
    	if ($screen < $lastBatch) {
    		$newQueryString = "$queryString&amp;" . SEARCH_GROUP . "=" . ($screen + 1);
    		$link = $_SERVER['PHP_SELF'] . $newQueryString;
    		$output = "<a href=\"" . $link . "\">Next</a>";
    		print($output . "&nbsp;\n");		
    	}

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    $_SERVER['PHP_SELF'] just refers to the current script and is probably being ignored by your rewrite rule. You also probably should be using $_GET, not $_SERVER['REQUEST_URI']. But it depends on whether the URL the user is actually seeing is OK. Also, $link should be to the "friendly" URL, not to the URL the server handles, which I think is what your current code is doing.

    Can you post examples of the URL you are after, the URL that is appearing and the right URL with query string appended? Also post the mod_rewrite rewrite rule(s) you're using.

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    with $_SERVER['PHP_SELF']
    1&nbsp;
    |&nbsp;&nbsp;<a href="index.php/?&amp;sGroup=2">2</a>&nbsp;
    |&nbsp;&nbsp;<a href="index.php/?&amp;sGroup=3">3</a>&nbsp;
    |&nbsp;&nbsp;<a href="index.php/?&amp;sGroup=4">4</a>&nbsp;
    |&nbsp;&nbsp;<a href="index.php/?&amp;sGroup=5">5</a>&nbsp;
    |&nbsp;&nbsp;<a href="index.php/?&amp;sGroup=6">6</a>&nbsp;
    |&nbsp;&nbsp;
    <a href="/cruise-destinations/caribbean-cruises/?&amp;sGroup=2">Next</a>


    with $_SERVER['REQUEST_URI']
    1&nbsp;
    |&nbsp;&nbsp;<a href="/cruise-destinations/caribbean-cruises/?&amp;sGroup=2">2</a>&nbsp;
    |&nbsp;&nbsp;<a href="/cruise-destinations/caribbean-cruises/?&amp;sGroup=3">3</a>&nbsp;
    |&nbsp;&nbsp;<a href="/cruise-destinations/caribbean-cruises/?&amp;sGroup=4">4</a>&nbsp;
    |&nbsp;&nbsp;<a href="/cruise-destinations/caribbean-cruises/?&amp;sGroup=5">5</a>&nbsp;
    |&nbsp;&nbsp;<a href="/cruise-destinations/caribbean-cruises/?&amp;sGroup=6">6</a>&nbsp;
    <a href="/cruise-destinations/caribbean-cruises/?&amp;sGroup=2">Next</a>

    but with the 2nd option, it doesn't strip the search string, it appends to it.

    the mod rewrite is via .htaccess:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    Thanks for any help or direction you might have.

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think you should make your URLs of this format:
    HTML Code:
    <a href="/cruise-destinations/caribbean-cruises/2/">2</a>
    And stop using &nbsp; all over the place! That's what margins and padding are for. Use CSS.

    Then your rewrite rule:
    Code:
    RewriteEngine On
    RewriteBase /
    RewriteCond &#37;{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([-\w/]+)/?$ index.php?u=$1 [L]
    Then index.php will have a query string appended to it (which only the server sees) like this:
    Code:
    index.php?u=cruise-destinations/caribbean-cruises/2/
    Then in your script you access that like this:
    PHP Code:
    $querystring $_GET['u']; // sanitize $querystring to make sure it contains something you expect
    $querybits explode('/'$querystring); // split the query string
    $page array_pop($querybits); // use each bit, e.g. this one assigns the number (2 in this case) to $page 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •