Special .htaccess rule for products from API

Hi there again peeps,

I am currently trying to make a site that simply displays some products, which you can click and view the details of the product. The products come from an (external) API.

What I’m trying to achieve:

For example, if I have the url http://example.com/products/detail/?name=powerbank&id=12345 I would like it, for SEO purposes, to something like http://example.com/products/detail/12345/powerbank/ or http://example.com/products/detail/12345-powerbank/

Attempt:

# Rewrite for detail links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^detail/(.*)/(.*)$ index.php?name=$2&id=$1 [L]

# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

The issue:

Well, since kirby has all links rewritten to the index.php I thought it’d be a good idea to do this as well.
However, obviously, kirby now not know what this url means. Is there any way to make it clear that the last two parameters “do not count”, at least for the site. I find it difficult to explain but I hope you htaccess and kirby guru’s understand me :grin:

How are you bringing the data in to Kirby exactly? How is the site actually set up? Is it just like a JSON import from a third party and ur simply displaying the info? Or some other way?

I’m using a cURL request in php to a “third” party. So yeah it is basically a JSON import, just displaying the info. The site structure is the following (simplified):

- root (home)
   '-> "Info in between page"                                                              ' 
    '-> list of products (Imports a filtered JSON, for example products, 12345, 12346 etc.)'
     '-> detail page of product 12345 (imports JSON for detailed product information)      '

Clear enough?

And from what do the query strings result? From a query in a controller? If so, you can set up routes with your clean URLS and then pass the information from the route to the controller for filtering. Dealing with that in the .htaccess doesn’t make sense.

Actually, the page that gives the overview requests all data with an ajax request in javascript, whereas the detail page requests the data directly using cURL (yes, from a controller)

^ I totally agree after all, realised this halfway through but I didn’t want to just throw my effort away and hoped it would work anyway somehow :stuck_out_tongue_winking_eye:

I looked in to routing just now and that seems to be the solution for me! I’m not sure if I understand the ‘action’, though.

A wild guess, if I’d want to route this url: example.com/info/products/detail/12345/powerbank, can I do something like this?:

[
  'pattern' => 'detail/(:num)/(:all)/',
  'action'  => function($num, $all) {
    // how can I pass these parameters, like this? 
    return [
       'page' => page("detail"),
       'id' => $num,
       'name' => $all
    ]
  }
]

Or rather something like:

[
  'pattern' => 'detail/(:num)/(:all)/',
  'action'  => function($num, $all) {
    // how can I pass these parameters, like this? 
    return new Page([
      'slug' => 'detail',
      'template' => 'detail',
      'content' => [
        'title' => 'Title of the detail page',
        'text'  => 'Believe it or not, this page is not in the file system',
        'id' => $num,
        'name' => $all
      ]
    ]);
  }
]

Your pattern needs the complete path, in this case info/products/detail/(:num)/(:all).

Your action has to return something, see the docs: https://k2.getkirby.com/docs/developer-guide/advanced/routing/#actions

That section also has how to return additional data (in that case your variables) to the controller. In the controller itself, you need to set the fourth parameter, see https://k2.getkirby.com/docs/developer-guide/advanced/controllers#arguments-from-the-router

1 Like

Ah, no, that is not possible. You can’t create a virtual page object like this, in Kirby 2, pages are tied to the file system. If you want virtual pages, you need Kirby 3.

I already have the router, controller solution working. Thanks for the help!
I do plan on upgrading sooner or later, though. Will this still be supported or should I use the virtual page solution in Kirby 3?

Are you trying to tell me that return new Page([]) worked? The code above can’t possible work as the PageAbstract constructor needs the parent page as first argument…

You can still use routes in Kirby 3 like in Kirby 2. But you have a lot more possibilities in general.

Haha no no! :wink:
That most definitely doesn’t work.

I meant this solution:

Ok, then I regard this as solved…