<p>You could add a check at the top of all of your templates that tests if the first uri segment matches one the agent names.</p>

<pre><code>{% set firstSegment = craft.request.firstSegment %}
{% set agents = craft.users.group('agents') %}
{% for agent in agents %}
    {% set agentName = agent.firstName ~ "-" ~ agent.lastName %}
    {% if firstSegment == agentName %}
        {% set requestedAgent = agent %}
    {% endif %}
{% endfor %}

{% if requestedAgent is defined %}
    {% include 'users/_profile' with { user: requestedAgent } only %}
{% else %}
    {% extend 'layouts/_main' %}

    ... normal page template goes here

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

<p>In your profile template you should now have a variable 'user' available with the requested agent.</p>

<pre><code>{% block content %}
    &lt;h1&gt;{% user.firstName %} {% user.lastName %}&lt;/h1&gt;
    &lt;p&gt;...&lt;/p&gt;
{% endblock %}
</code></pre>

CC BY-SA 3.0
2017-02-04T19:18:12.607Z
That worked well. Here's what I ended up doing.

`{% set aname = craft.request.firstSegment %}
{% if aname %}
  {% set agentObj = craft.users.username(aname).search('publicProfile:1').last() %}
  {% if agentObj | length %}{% redirect 'agent/' ~ agentObj.username | lower %}
  {% endif %}
{% endif %}`
Philip Jones
CC BY-SA 3.0
2017-02-04T19:19:22.143Z
I really hate that you can't format code in comments.
Philip Jones
CC BY-SA 3.0
2017-01-03T07:36:23.983Z
2017-01-03T07:45:54.930Z
2017-01-03T07:45:54.930Z
Douglas McDonald
<p>I am building a site for a travel agency. Each agent needs to have his/her own public profile page. I want each agent's profile page to be accessed at <code>agencyname.com/agentname</code>, with <code>agentname</code> being the agent's username. This would load an agent profile template populated with the given agent's profile data.</p>

<p>I still need other non-agent segment-1 pages to continue working also, like <code>agencyname.com/cruises</code> or <code>agencyname.com/group-travel</code>. These would be 'normal' template pages.</p>

<p>Is there any way to make this work dynamically? Creating a dynamic route with only a token in Segment 1 breaks access to other regular pages. I don't want to have to create a hard route every time we add an agent, and we don't want to add another segment like <code>agencyname.com/agent/agentname</code>. We want the URL each agent puts on their business card to be as clean and easy to remember as possible.</p>

<p>I'm thinking something along the lines of this logic:</p>

<pre><code>If Segment 1 matches a username
   Display the profile template for that user
Else
   Continue routing per the normal routing checks
</code></pre>

<p>I don't have the knowledge to build a plugin, or the budget for someone else to. I'm seeking solutions that could be implemented without a plugin (unless there's already a plugin available).</p>

<p>Any constructive thoughts are appreciated.</p>

<routes><user-profile>
Route to User page from first segment
# Route to User page from first segment

(Asked by: username_0 on 2017-01-02)

I am building a site for a travel agency. Each agent needs to have his/her own public profile page. I want each agent's profile page to be accessed at agencyname.com/agentname, with agentname being the agent's username. This would load an agent profile template populated with the given agent's profile data.
I still need other non-agent segment-1 pages to continue working also, like agencyname.com/cruises or agencyname.com/group-travel. These would be 'normal' template pages.
Is there any way to make this work dynamically? Creating a dynamic route with only a token in Segment 1 breaks access to other regular pages. I don't want to have to create a hard route every time we add an agent, and we don't want to add another segment like agencyname.com/agent/agentname. We want the URL each agent puts on their business card to be as clean and easy to remember as possible.
I'm thinking something along the lines of this logic:
If Segment 1 matches a username
   Display the profile template for that user
Else
   Continue routing per the normal routing checks

I don't have the knowledge to build a plugin, or the budget for someone else to. I'm seeking solutions that could be implemented without a plugin (unless there's already a plugin available).
Any constructive thoughts are appreciated.

**Tags:** routes, user-profile

---

## 1 answers

### Answer by username_1 on 2017-01-03. Score: 2 | Accepted answer

You could add a check at the top of all of your templates that tests if the first uri segment matches one the agent names.
{% set firstSegment = craft.request.firstSegment %}
{% set agents = craft.users.group('agents') %}
{% for agent in agents %}
    {% set agentName = agent.firstName ~ "-" ~ agent.lastName %}
    {% if firstSegment == agentName %}
        {% set requestedAgent = agent %}
    {% endif %}
{% endfor %}

{% if requestedAgent is defined %}
    {% include 'users/_profile' with { user: requestedAgent } only %}
{% else %}
    {% extend 'layouts/_main' %}

    ... normal page template goes here

{% endif %}

In your profile template you should now have a variable 'user' available with the requested agent.
{% block content %}
    {% user.firstName %} {% user.lastName %}
    ...
{% endblock %}

> **Comments:**
> **username_0**: That worked well. Here's what I ended up doing.  `{% set aname = craft.request.firstSegment %} {% if aname %}   {% set agentObj = craft.users.username(aname).search('publicProfile:1').last() %}   {% if agentObj | length %}{% redirect 'agent/' ~ agentObj.username | lower %}   {% endif %} {% endif %}`  
> **username_0**: I really hate that you can't format code in comments.  

---

