Adjusting a php function so that it displays link

2020-05-07 04:07发布

问题:

I added the following .htaccess rule:-

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

So now I have a simple link called 'widgets' which leads to the 'widgets' category 1st page. However, the links to the 2nd page looks like the following:-

products.php?cat=20&pg=2

What I would like is for the subsequent pages to be rather in the form of:-

widgets?pg=2

The QSA flag in the above .htaccess rule does achieve this, but I need to change the function which generates these page links, otherwise the only way of getting to widgets?pg=2 is by typing it in the browser address bar as:- mywebsite.com/widgets?pg=2.

I think the following PHP function might need to be adjusted, to achieve the result I want. Can any PHP wizards or anyone with appropriate knowledge please help with this. The reason I want to do this is because I want google to index the simple looking pages, rather than the longer ones:-

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);
}

If it helps to know how the writepagebar function fits into the incproducts.php which itself sits inside the products.php page you can see here:-

http://freetexthost.com/3ubiydspzm

回答1:

Edited after comments from "nitbuntu":-

In the function just instead of these lines:-

    $sStr = str_replace(array('&amp;pg=1"','?pg=1"'),'" rel="start"',$sStr);
    return($sStr);
}

write the following lines:-

    $sStr = str_replace('products.php?cat=20', 'widgets', $sStr);
    $sStr = str_replace('&amp;pg=', '?pg=', $sStr);
    $sStr = str_replace(array('&amp;pg=1"', '?pg=1"'), '" rel="start"', $sStr);
    return($sStr);
}

Hope it helps.



回答2:

Analyzing your code, it seems that $sLink is who contains the 'products.php?cat=20' value, the function only appends the page value, so maybe you need to modify another function.

In other words, your writepagebar() only appends the page number to the generated link. You must search who is calling writepagebar() with the 'products.php?cat=20' string and modify at that level.