Route for all tag urls

My url to filter by tags used to be something like web.com/tag:Web%20elements, now they are like web.com/tag/web-elements. I’m trying to make a route from the all format to the new one. This is what i’m trying but it’s not working.

<?php
  return [
    'routes' => [
    [
        'pattern' => 'resources/tag:(:any)',
        'action'  => function($tag) {
          return go('resources/tag/' . Str::slug($tag));
        }
      ]

I think there is a problem with the :, because if the url is with /it works.

Parameters in routes are ignored, I think. You would have to listen to the main route, i.e. resources then inside the route, you would check for the request URL and react based on that.

As an alternative, you coult put a rewrite rule into your .htaccess.

I tried .htaccess like this, but no luck :thinking:

RewriteCond %{QUERY_STRING} ^tag:(.*)$
RewriteRule ^https://xd.test/resources/$ https://xd.test/resources/tag/%2

Those URLs look like local ones. Are you using Valet? That uses NGINX so htaccess wont fly locally.

As I already mentioned about, within a route, you can do this:

'routes' => [
      [
        'pattern' => 'resources',
        'action'  => function() {
          if ($tag = param('tag')) {
            return go('resources/tag/' . Str::slug($tag));
          } else {
            return page('resources');
          }
        }
      ],
]
1 Like

I forgot that! i’m new to valet. I used @texnixe’s solution, Thanks!