views:

48

answers:

4

Hi, I've added a .htaccess file to my root folder for the purposes of rewriting dynamic URLs into static ones. This seems to have been done successfully but I am having problems with page numbers.

For example, if you were to visit a static page /widgets, the first page of the products is fine....but subsequent pages show up as /products.php?cat=27&pg=2 etc. What I want is for subsequent pages to be in the form of /widgets-pg2 or /widgets?pg=2.

Below is my rewrite rule that I used for the initial category page:-

RewriteRule ^widgets$ products.php?cat=27

If any of you experts can help with this, it would be much appreciated.

+1  A: 

Try

RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1

After that, go here :)

zildjohn01
Tried this and it seemed to work for the /widgets but didn't work for /doodles or /other-stuff which resulted in a 'Not Found' error. Then a strange thing happened: when I changed the rewrite rule for 'widgets' back to the original one, saving and uploading it to the server seems to made no difference. It still works.
nitbuntu
Ignore the above message. The reason is that I had a separate widgets.php file which was being referred and caused a clash.
nitbuntu
If you want it to work for `/doodles` or anything else, you need to also add those rules in, one line per category.
zildjohn01
nitbuntu
A: 

To allow a query string after your rewritten URL use the [QSA] flag:

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

A link would than be:

http://example.org/widgets?pg=167&perpage=100&tralala=Hi!
nikic
It’s QSA and not QSM.
Gumbo
Thanks Gumbo, fixed.
nikic
The QSA creates the relevant pages, but these still need to be added as links. For this, the answer can be found here:- http://stackoverflow.com/questions/3422244/adjusting-a-php-function-so-that-it-displays-link-in-the-form-of-widgetspg2
nitbuntu
+1  A: 

Are you expecting the cat to change as well? You'd need to account for that in your URL as well:

e.g. www.site.com/widgets/27/2 could be rewritten as:

RewriteRule ^widgets/([0-9]+)/([0-9]+)$ products.php?cat=$1&pg=$2

If widgets will always be cat 27 then you can change it to:

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

which is query string append

David O.
nitbuntu
A: 

I tried the following but it resulted in a '404 Not Found' error when I go to /widgets:-

RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1

And I tried the following:-

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

This worked correctly but to go to page two of the widgets page, I need to type in the browser:-

/widgets?pg=2

But the actual 'page 2' link still leads to:-

products.php?cat=27&pg=2

So apart from a rewrite rule....maybe I need a separate redirection rule? Or maybe need to change the page number links. But since these are dynamically generated, I'm not sure how to do this.

The following is the PHP code for the page:- http://freetexthost.com/3ubiydspzm

nitbuntu