2

Is it possible to use dynamic urls in {segment_1}. I prefer not to use a plugin and might opt for a mod_rewrite solution but I am curious if this is possible out of the box.

For example... http://domain.com/url-title

To further elaborate, I want a url structure that will allow me to do this type of url structure.

I have looked into freebie, and it would work perfect for my needs. If there is a workaround for segment_1 removal with Freebie it would be nice to hear.

I also found a great schematic/flowchart for EE urls. might be helpful to others looking into this issue. EE Url Schematic

  • Looks like you're making some kind of SaaS app? If so have you considered using wildcard sub-domains instead? I tried it with EE and it worked quite nicely. Also thanks for the kind words on my old URL Schematic... I should update that at some point to account for the new Routes functionality... – James Smith Jun 6 '14 at 18:15
  • You are indeed correct, SaaS is in the works. I'll look into wildcard subdomains, thanks for the info. Sorry I didn't post your name on that Schematic :) – Mike McBrien Jun 6 '14 at 19:30
3

ExpressionEngine 2.8's introduced Template Routes so you now have more control over how your templates are processed. It allows you to override the default template route, which is /template_group/template.

For example instead of going /blog/read/url_title you can now add a template route for read.html in the blog template group to load like /blog/url_title - skipping that middle segment.

However, what you are asking for wouldn't be possible with Template Routes. That's because if you have a dynamic segment in your route then that route must end with a dynamic segment.

So for example: /blog/{entry_title}/ is accepted, but /blog/{entry_title}/preview is not because the dynamic variable ({entry_title}) is BEFORE the final variable which isn't dynamic.

If we take your desired URLs they would look like this in template routes:-

  • _view/index.html -> /{entry}/
  • _admin/index.html -> /{entry}/admin/
  • _admin/events.html -> /{entry}/admin/events/
  • _admin/communicate.html -> /{entry}/admin/communicate/

BUT as you can see, it relies on the last segment being non-dynamic and so won't work natively in ExpressionEngine.

Trying to subvert how ExpressionEngine natively 'thinks' could lead to unnecessary complex code and the same basic result.

I would suggest that it's not worth the hassle or complexity trying to achieve your desired URL scheme and you would be better off doing the natively possible:-

  • _view/index.html -> /{entry}/
  • _admin/index.html -> /admin/{entry}/
  • _admin/events.html -> /admin/events/{entry}/
  • _admin/communicate.html -> /admin/communicate/{entry}/

With all that said, it would be great if Template Routes allowed for this, and perhaps it's something they'll implement later on.

| improve this answer | |
  • Fantastic write up, this helps my understanding of Template routes, but more specifically explains why I couldn't get it to work - because it wont for my purpose, Caveats FTW! – Mike McBrien Jun 9 '14 at 17:54
2

As long as you don't have a template or template group named that url title, you can code your index template to take in {segment_1} as a url_title.

{exp:channel:entries channel="foo" {if segment_1}url_title="{segment_1}" dynamic="no"{/if}}

edit: This only works if you set strict url's to no

| improve this answer | |
  • 1
    Can't routes be a solution? I'll not make a specific suggestion because I'm fighting the routes specification these days. – Robson Sobral Jun 5 '14 at 19:31
  • Thanks for the Answer Johnathan, You solution works but has the caveat of still showing entries when no segment is used, and you have to disable strict urls (which you posted when I was replying :)) – Mike McBrien Jun 5 '14 at 19:56
  • Routes is a good solution as long as you are not using core. – Johnathan Waters Jun 5 '14 at 20:00
  • i'm not using core - routes kinda confuse me - how would I accomplish this with routes? – Mike McBrien Jun 5 '14 at 20:17
  • I added some more information in the original post - thanks so far with help. – Mike McBrien Jun 6 '14 at 14:46
0

The final result of my trials and errors...

Freebie does indeed work on segment_1 however your index template will also have to handle the routing if you have dynamic urls.

You can still use all the segment variables you want with Freebie, HOWEVER, if you use freebie to "remove" segments of the URL, they won't be accessible as segment 1.

For example, if I have the URL:

/foo/bar/baz

and I use Freebie to remove foo and baz, {segment_1} will return bar, and {segment_2} and {3} will return nothing. (You can access these vars as {freebie_1-3}).

(thank you to Doug Avery for the explanation of how freebie handles this)

Because my desired URL scheme cannot be accomplished completely dynamically with Freebie or Template Routes I looked at another slightly more manual approach.

Using the highly regarded Switchee plugin i have set up a case statement on my main index page.

Here is the code that I am using to achieve my desired url paths...

{if segment_1 == ''}
This is the home page.
{/if}

{if segment_1 != ''}
{exp:channel:entries limit="1" dynamic="no" url_title="{segment_1}"}
    {exp:switchee variable="{segment_2}" parse="inward"}
        
        {case value=""}
            {if no_results} {redirect="404"} {/if}
            <h2>{title}</h2>
            <p>This is the main users page</p>
        {/case}
        
        {case value="admin"}
            {if no_results} {redirect="404"} {/if}
            <h2>{title}</h2>
            <p>This is the admin page</p>
        {/case}
        
        {case value="signup"}
            {if no_results} {redirect="404"} {/if}
            <h2>{title}</h2>
            <p>Signup Page</p>
        {/case}
        
    {/exp:switchee}
{/exp:channel:entries}
{/if}

A few notes, I did turn off strict urls to achieve 404 pages on non existing elements and will likely use Causing Effect's new 'In' add on to help keep template fast and clean for each of the subpages. I will also need to use conditionals, or case statements for additional url schemes on the site.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.