WordPress.org

Ready to get started?Download WordPress

Forums

Ignoring &page in the URL (8 posts)

  1. rr89
    Member
    Posted 1 year ago #

    Hi,

    Is it possible to ignore &page in the URL to disable pagination for specific pages?

    I have a WordPress site which is integrating with a 3rd party PHP script which pulls info from their servers and that uses &page in the URL to return nested results, this breaks however as WordPress takes it as being page /2/, /3/, etc.

    I've tried to disable pagination on it's own via functions.php however it still takes the &page from the URL and puts /2/ in the URL.

    Thanks.

  2. linux4me2
    Member
    Posted 1 year ago #

    Can you just modify the 3rd party PHP script to remove the "&page" from the URLs using either a regular expression with preg_replace or by parsing the query string and removing the "page" value?

    For example with preg_replace():

    $pattern = '/&page(\=[^&]*)?(?=&|$)|^page(\=[^&]*)?(&|$)/';
    $url = preg_replace($pattern, '', $url);

    I struggle with regular expressions myself, but that looks like it would replace page both if it is not the first parameter in the query string and if it is. You'll have to modify it if the script is actually passing the HTML-encoded version of an ampersand instead of just the ampersand alone.

  3. rr89
    Member
    Posted 1 year ago #

    Hi,

    Thanks for the help linux4me2.

    The &page value needs to be there. It's not in all the URLs, only when you go to another page in the results it returns. It'll probably help if I explain what it does.

    The website is for real estate searches and the third party company holds all the properties in their database. Instead of nesting inside an iframe they have provided some PHP which I've nested inside custom WP page templates. Everything works fine, except if you go to page 2 of the results it passes &page=2 in the URL and WordPress takes this and assumes /2/, and &page=2 is stripped from the URL - meaning you don't actually get the second page of results, it stays on the first. Other arguments passed such as &maxPrice= and &city= work fine as you might expect.

    So what I was looking to do is work around that by getting WP to ignore &page on these custom page templates to allow the results to span various pages.

    Thanks again.

  4. linux4me2
    Member
    Posted 1 year ago #

    I'm not certain I get it yet. Are you saying you are using SEO-friendly permalinks so your custom listings page URL looks something like:
    http://yourdomain.com/listings
    and when your users try to view page two of the listings the link that WordPress is creating looks something like:
    http://yourdomain.com/listings/2/?maxPrice=XXXXXX&city=XXXXXX
    but you want it to be:
    http://yourdomain.com/listings/?page=2&maxPrice=XXXXXX&city=XXXXXX
    If that's correct, then one way is to alter the redirect you have in your .htaccess for permalinks so that when "listings" is in the URL it rewrites the query string so that the page parameter is sent like "page=2" the way you want. The other way would be to alter the PHP in the custom page templates so it interprets the URL correctly.

    For the former, you're going to need to post the URLs the way they are, and the way they should look, along with the current redirects in your .htaccess file so that someone can help you come up with the correct syntax.

    For the latter, we're going to have to see the code block that receives the URL in order to suggest how to alter it.

  5. rr89
    Member
    Posted 1 year ago #

    Hi,

    Your first example is exactly correct. In this case it works like;

    Listings page: http://domain.com/properties/ - shows everything.

    Page 2 Target URL: http://domain.com/properties/?eaid=1111&branchList=&minPrice=50000&maxPrice=9999999&bedrooms=0&showSTC=true&searchAllAddress=&sa1=&sa2=&searchTown=&city=&county=&postcode=&location=&classification=&propertyType=&sortDescending=&perPage=25&page=2&apikey=1AAAA111-11A1-111A-1111-1111A11A1111&rentalperiod=0

    Page 2 WP generated URL: http://domain.com/properties/2/?eaid=1111&branchList&minPrice=50000&maxPrice=9999999&bedrooms=0&showSTC=true&searchAllAddress&sa1&sa2&searchTown&city&county&postcode&location&classification&propertyType&sortDescending&perPage=25&apikey=1AAAA111-11A1-111A-1111-1111A11A1111&rentalperiod=0

    I've substituted some values there such as the api key.

    So we would basically need to create a .htaccess rule which only applies when /properties/ is part of the URL?

    Thanks.

  6. linux4me2
    Member
    Posted 1 year ago #

    If I'm following you, yes, that would mean WordPress would still be generating the URLs with the "/2" in there, but when the user clicks on them, mod_rewrite would get hold of it and change it from this:
    http://domain.com/properties/2/?eaid=1111&branchList&minPrice=50000&maxPrice=9999999&bedrooms=0&showSTC=true&searchAllAddress&sa1&sa2&searchTown&city&county&postcode&location&classification&propertyType&sortDescending&perPage=25&apikey=1AAAA111-11A1-111A-1111-1111A11A1111&rentalperiod=0
    to this:
    http://domain.com/properties/?eaid=1111&branchList=&minPrice=50000&maxPrice=9999999&bedrooms=0&showSTC=true&searchAllAddress=&sa1=&sa2=&searchTown=&city=&county=&postcode=&location=&classification=&propertyType=&sortDescending=&perPage=25&page=2&apikey=1AAAA111-11A1-111A-1111-1111A11A1111&rentalperiod=0
    which is what the URL would be coming into your script, as if the user had pasted the second one into the address bar of their browser. Would that work?

    If so, what you'd essentially be doing is having mod_rewrite take what looks like a subdirectory and changing it into a query string parameter called "page." I know mod_rewrite can do that, but it's beyond my limited abilities with mod_rewrite.

    I think what you're going to want is something like this integrated with your current conditions/rules for your permalinks:

    RewriteEngine On
    RewriteRule ^(?properties/)(.*)$ properties/?page=$1 [QSA,L]

    but I'm not sure I have the "properties" part right. What that's saying (I hope) is that if "properties" is in the path, add the name of the subfolder on as the value of the "page" parameter, then append the rest of the query string (that's what the "QSA" is for).

    You'll have to try it and see if your entire site explodes and let me know. If it doesn't work, you might try posting over on Stack Overflow in the mod_rewrite section. If you give them the current URL and the intended URL along with this Forrest Gump attempt, I think they'll help you. Either way, please post back here because I'd like to know what works out. : )

  7. rr89
    Member
    Posted 1 year ago #

    Hi,

    Many thanks for all your efforts. I'll give it a try after the coffee sets in and I'm awake! Will post back with the results :)

    Thanks

  8. rr89
    Member
    Posted 1 year ago #

    The suppliers of the 3rd party stuff have provided this function which works,

    add_filter('request', 'on_request_na');
    
    function on_request_na($vars)
    {
      unset($vars['page']);
      return $vars;
    }

    I'm going to continue to look at the rewrite rule however, so I can control this for just the Property pages rather than turning off pagination site wide.

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic