views:

46

answers:

4

I have an e-commerce website (created with a ecommercetemplates shopping cart PHP template) and on the catalogue pages there are, at times, a few pages which are numbered at the bottom. The links to these pages are, for example, in the form:- /product.php?cat=27&pg=2 despite the main page having been mapped to:- /widgets using a rewrite rule in a .htaccess file.

For the sake of consistency and simplicity, I would like the pages to be as:- /widgets?pg=2. I asked in this forum how to do this and found the following solution:-

RewriteRule ^widgets$ products.php?cat=27 [QSA] 

The problem is that despite the above rewrite rule working as I wanted, the current links on the catalogue page still point to, for example:- /product.php?cat=27&pg=2. In order to go to /widgets?pg=2, I have to enter this in the browser.

So my question is: How do I change the current links on the catalogue page, which are dynamically generated? Are there any further rules which can be entered in .htaccess or is there any other solution to this problem? The following is the PHO code for the product page:- http://freetexthost.com/3ubiydspzm.

In the link above, if you do a search for 'writepagebar', I think this is where the 'next page', 'previous page', and the 'page numbers' are done.

A: 

You have a couple of options:

Most of them require more than just .htaccess rewrite rules.

  • You can modify the writepagebar function to output the friendly URL for you. (And change any other function that might link to this products page.)
  • You can buffer the entire page's output and do a string replace on it. (See ob_start and ob_get_clean in the PHP manual.)
  • You can redirect ugly URLs to pretty ones with a 301 redirect. (The links will stay the same, but your friendly URLs get the SEO credit and show up in the browser.)
    • Or you can try this ugly mod_rewrite hack that I can't guarantee will work on your server or with your application.

Hack:

RewriteCond %{QUERY_STRING} (.*)(cat=27\&)(.*) [OR]
RewriteCond %{QUERY_STRING} (.*)(cat=27)(.*)
RewriteCond %{THE_REQUEST} !widgets
RewriteRule ^products.php$ widgets?%1%3 [R=301,L]

(This might need the rewrite base option)
http://services.rrbits.com/products.php?cat=27&pg=2 (Example link.)

EPB
I thought adjusting the writepagebar function might be worth a try. Any ideas how to adjust it to get the result that I want? I've posted the code in one of the answers.
nitbuntu
A: 

Could a solution to this be that I copy the products.php page and rename it to, for example, widgets.php etc. Then the pages will automatically be written in the form of:- /widgets.php?pg=2. I can then use .htaccess to remove the '.php' part so that it becomes simply:- /widgets?pg=2?

If this is a solution, could someone tell me what I'd need to put in .htaccess to remove the .php from widgets.php.

nitbuntu
A: 

I thought my best chance is to adjust the writepagebar function. Since I don't know much about php...I thought I'd post the code here, and someone can have a look at it and maybe help adjust it to get the affect that I need.

The code is as follows:-

function writepagebar($CurPage,$iNumPages,$sprev,$snext,$sLink,$nofirstpage){
    $startPage = max(1,round(floor((double)$CurPage/10.0)*10));
    $endPage = min($iNumPages,round(floor((double)$CurPage/10.0)*10)+10);
    if($CurPage > 1)
        $sStr = $sLink . '1' . '" rel="prev"><span style="font-family:Verdana;font-weight:bold">&laquo;</span></a> ' . $sLink . ($CurPage-1) . '">'.$sprev.'</a> | ';
    else
        $sStr = '<span style="font-family:Verdana;font-weight:bold">&laquo;</span> '.$sprev.' | ';
    for($i=$startPage;$i <= $endPage; $i++){
        if($i==$CurPage)
            $sStr .= '<span class="currpage">' . $i . '</span> | ';
        else{
            $sStr .= $sLink . $i . '">';
            if($i==$startPage && $i > 1) $sStr .= '...';
            $sStr .= $i;
            if($i==$endPage && $i < $iNumPages) $sStr .= '...';
            $sStr .= '</a> | ';
        }
    }
    if($CurPage < $iNumPages)
        $sStr .= $sLink . ($CurPage+1) . '" rel="next">'.$snext.'</a> ' . $sLink . $iNumPages . '"><span style="font-family:Verdana;font-weight:bold">&raquo;</span></a>';
    else
        $sStr .= ' '.$snext.' <span style="font-family:Verdana;font-weight:bold">&raquo;</span>';
    if($nofirstpage) $sStr = str_replace(array('&amp;pg=1"','?pg=1"'),'" rel="start"',$sStr);
    return($sStr);
}
nitbuntu