使用.htaccess或任何其他方法调整动态生成的链接?

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.

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.)