Can you help me figure how to clean an url with pagination?

Hi,

This is actually about PHP and .HTACCESS with pagination. I’m trying to use clean urls because they’re SEO friendly and nicer to look at. This is how the original url looks (localhost):

http://localhost/mywebsite?listing=7939844

And after using this HTACCESS rewrite rule,
RewriteRule ^listing/?([0-9]+) listing.php?listing=$1 [L,NC]
this is how the url looks:
http://localhost/mywebsite/listing/7939844

That’s all working perfectly. Now I started using pagination (a library called Zebra Pagination) and clicking on the next page changes the url. Currently this is what it does:
http://localhost/pedlar/listing/7939844?listing=7939844&page=2
(incorrect, plus it doesn’t go to page 2, it stays on page 1!)

On a normal test page without a clean url, this is what the pagination library adds to the url: ?page=2, that’s it.

How should I alter the rewrite rule to make the url look like this, or a working version?
http://localhost/pedlar/listing/7939844/page-2

My dozenth attempt (and fail):
RewriteRule ^listing/?([0-9]+)&page/([0-9]+) listing.php?listing=$1&page=$2 [L,NC]

In my mind, a listing with an id would go to a specific page, not a list?

RewriteRule ^listing/(\d+)/page/(.+)/?$ listing.php?id=$1&page=$2 [NC,L]

maybe?

Yes, going to the listing page with an ID displays the listing, plus a separate div that selects all other listings by that user, so pagination for that ‘div’ is required.

Let me give your rule a try

It does the same thing :confused: It gives the url those extra parameters like so,
http://localhost/pedlar/listing/7939844?listing=7939844&page=2

Based on how it should function, this isn’t what you want to do.

First, and it depends on technology knowledge, I would convert this to a front end system like react of angular.

Because the learning curve is steep if you don’t know those, I would use Ajax and an endpoint for the simpler version.

It’s probably on another level… I’ve got a quick fix since this project is way too large to convert to a front end system. Instead of using pagination i’ll set a maximum height and give the div a vertical scroll bar. Not very pretty but it’s the only option for me.

Thank you for stopping by :slight_smile:

Let me see if I understand correctly.

http://localhost/mywebsite?listing=7939844 goes to a page showing the full details of a single record but also shows a list of other things owned by the same user as the one who owns #7939844.

There are dozens of other records owned by this same user and so the list of other records needs to be paginated.

Is that correct?

Why does that list need to be on that page? Can you not have a “see others” link that goes to the main search results area that already has pagination and shows all records owned by that user?

Personally would try having only a dozen additional records listed on the page and then a “more” link that goes to page 2 of the main search results area. I assume you already have a main search results page that already has pagination.

I would also suggest using a routing library instead of struggling with .htaccess mod_rewrite and regular expressions.

$router->respond('/listing/[i:listing_id]/[i:page]?', function() {
	// The integer variables 
	// $listing_id and $page (optional)
	// are used by listing.php code.
	include 'listing.php';
	return;
});

And while pretty urls are nice, having just ?page=2 as the only param isn’t the ugliest thing in the world.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service