<p>You can do this completely in the template. Have the structure entries resolve to a route like <code>gallery/_router</code> and then inside that template you could use this code to include the template of your choice:</p>

<pre><code>{% switch craft.app.request.getParam('type') %}

    {% case "gallery" %}

        {% include "gallery/_entry" %}    

    {% case "anotherParam" %}

        {% include "gallery/_anotherParam" %}

     {% default %}

        {% include "gallery/_default" %}   

{% endswitch %}
</code></pre>

CC BY-SA 4.0
2019-04-15T10:28:48.583Z
Problem is that the page where the ?type parameter is, will generate a 404 error when you don't specify a route... I edited the question to better explain the situation...
outline4
CC BY-SA 4.0
2019-04-12T21:55:48.983Z
2019-04-12T21:55:48.983Z
Alan Miller
<p>I have a main structure as my navigation. 
Within this structure I have multiple image galleries. 
I want to make the image gallery's urls dynamic. </p>

<p>If I define a gallery within the structure entry:<br>
<a href="https://mysite.com/sport/gallery/" rel="nofollow noreferrer">https://mysite.com/sport/gallery/</a>  </p>

<p>The url of the images in the gallery (which is a channel) should be:<br>
<a href="https://mysite.com/sport/gallery/mygallerySlug?type=gallery&amp;parent=parentEntryId" rel="nofollow noreferrer">https://mysite.com/sport/gallery/mygallerySlug?type=gallery&amp;parent=parentEntryId</a></p>

<p>now I would like to have a route which catches all of the ?type=gallery entries and load the "gallery/_entry" template. </p>

<p>It shouldn't matter how deep in the structure the ?type=gallery is. </p>

<p>I don't seem to be able to do this within the CP, and I've never used advanced routing... </p>

<p>Any help appreciated! </p>

<p>EDIT:</p>

<p>Maybe I need to specify a little further: </p>

<p>I have a structure called "pages" which acts as the main structure.<br>
I have a channel that is called "galleries" where I can upload images and assign a category. 
In my main structure I can assign galleries from within a matrix block and choose a category. That way I can regroup multiple entries from the channel "galleries" in a structure entry. </p>

<p>The urls of the galleries should be completely dynamic: </p>

<p><a href="https://mysite.com/sport/gallery" rel="nofollow noreferrer">https://mysite.com/sport/gallery</a> holds multiple gallery entries where each of these galleries should have a url the following way:<br>
<a href="https://mysite.com/sport/gallery/gallery1" rel="nofollow noreferrer">https://mysite.com/sport/gallery/gallery1</a><br>
<a href="https://mysite.com/sport/gallery/gallery2" rel="nofollow noreferrer">https://mysite.com/sport/gallery/gallery2</a> ...</p>

<p>another gallery collection could be here: 
<a href="https://mysite.com/karting/galleries/" rel="nofollow noreferrer">https://mysite.com/karting/galleries/</a> 
whith links like that: 
<a href="https://mysite.com/karting/galleries/gallery3" rel="nofollow noreferrer">https://mysite.com/karting/galleries/gallery3</a><br>
<a href="https://mysite.com/karting/galleries/gallery4" rel="nofollow noreferrer">https://mysite.com/karting/galleries/gallery4</a><br>
etc..</p>

<p>I know I could specify the entry url of one gallery the following way in the "galleries" section url settings: </p>

<p>{{ craft.entries.id(65).locale('de').first.uri }}/{slug}<br>
where the id is the id of e.g. <a href="https://mysite.com/sport/gallery/" rel="nofollow noreferrer">https://mysite.com/sport/gallery/</a> structure entry. </p>

<p>But that works only for one gallery.</p>

<p>I could create a route for "sport/gallery/*" and assign the "gallery/_entry" template to it. But that way I would have to create a route for every gallery, which is not completely obvious, nor intuitive on a editor's point of view. </p>

<p>In fact this is almost a duplicate of this question:<br>
<a href="https://craftcms.stackexchange.com/questions/19204/dynamic-urls-for-channel-structure-thats-linked-to-the-main-structure">dynamic urls for channel/structure that&#39;s linked to the main structure</a></p>

<p>Cheers<br>
Stefan</p>

CC BY-SA 4.0
2019-04-12T21:55:55.837Z
You don't have to do this in the control panel. You could do it completely in the template. Why do you want to load a specific template based on the query string when you already have "gallery" in the url structure? Not trying to be mean genuinely curious about your use case!
Alan Miller
CC BY-SA 4.0
2019-04-15T10:27:14.233Z
problem is hitting that  "gallery/_entry" template. If I don't define a route, it will generate a 404 page.
outline4
<routes><routing><querystring>
How to create an advanced route with query strings?
# How to create an advanced route with query strings?

(Asked by: username_0 on 2019-04-12)

I have a main structure as my navigation.
Within this structure I have multiple image galleries.
I want to make the image gallery's urls dynamic.

If I define a gallery within the structure entry:  
<https://mysite.com/sport/gallery/>

The url of the images in the gallery (which is a channel) should be:  
<https://mysite.com/sport/gallery/mygallerySlug?type=gallery&parent=parentEntryId>

now I would like to have a route which catches all of the ?type=gallery entries and load the "gallery/\_entry" template.

It shouldn't matter how deep in the structure the ?type=gallery is.

I don't seem to be able to do this within the CP, and I've never used advanced routing...

Any help appreciated!

EDIT:

Maybe I need to specify a little further:

I have a structure called "pages" which acts as the main structure.  
I have a channel that is called "galleries" where I can upload images and assign a category.
In my main structure I can assign galleries from within a matrix block and choose a category. That way I can regroup multiple entries from the channel "galleries" in a structure entry.

The urls of the galleries should be completely dynamic:

<https://mysite.com/sport/gallery> holds multiple gallery entries where each of these galleries should have a url the following way:  
<https://mysite.com/sport/gallery/gallery1>  
<https://mysite.com/sport/gallery/gallery2> ...

another gallery collection could be here:
<https://mysite.com/karting/galleries/>
whith links like that:
<https://mysite.com/karting/galleries/gallery3>  
<https://mysite.com/karting/galleries/gallery4>  
etc..

I know I could specify the entry url of one gallery the following way in the "galleries" section url settings:

{{ craft.entries.id(65).locale('de').first.uri }}/{slug}  
where the id is the id of e.g. <https://mysite.com/sport/gallery/> structure entry.

But that works only for one gallery.

I could create a route for "sport/gallery/\*" and assign the "gallery/\_entry" template to it. But that way I would have to create a route for every gallery, which is not completely obvious, nor intuitive on a editor's point of view.

In fact this is almost a duplicate of this question:  
[dynamic urls for channel/structure that's linked to the main structure](https://craftcms.stackexchange.com/questions/19204/dynamic-urls-for-channel-structure-thats-linked-to-the-main-structure)

Cheers  
Stefan

**Tags:** routes, routing, querystring
> **Comments:**
> **username_1**: You don't have to do this in the control panel. You could do it completely in the template. Why do you want to load a specific template based on the query string when you already have "gallery" in the url structure? Not trying to be mean genuinely curious about your use case!  
> **username_0**: problem is hitting that  "gallery/_entry" template. If I don't define a route, it will generate a 404 page.  

---

## 1 answers

### Answer by username_1 on 2019-04-12. Score: 2

You can do this completely in the template. Have the structure entries resolve to a route like gallery/_router and then inside that template you could use this code to include the template of your choice:
{% switch craft.app.request.getParam('type') %}

    {% case "gallery" %}

        {% include "gallery/_entry" %}    

    {% case "anotherParam" %}

        {% include "gallery/_anotherParam" %}

     {% default %}

        {% include "gallery/_default" %}   

{% endswitch %}

> **Comments:**
> **username_0**: Problem is that the page where the ?type parameter is, will generate a 404 error when you don't specify a route... I edited the question to better explain the situation...  

---

