<p>I would not particularly advise creating a uri pattern that was just 'www.site.com/username' because usernames can essentially be anything which might cause routing conflicts (i.e. if the username is 'index' for example). You could however use something like 'www.site.com/members/username' or 'www.site.com/u/username'.</p>

<p>One way to do this would be to add a route using a 'subpattern'. For more info check out the routing docs on <a href="http://buildwithcraft.com/docs/routing#accessing-subpatterns-in-your-templates">accessing subpatterns in your templates</a>.</p>

<p>Here is the example given in the docs, which provides the variables 'year' and 'month' to the template that correspond to the matched uri segments (i.e.  `news/2015/02'):</p>

<pre><code>'news/(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;\d{2})' =&gt; 'news/_archive',
</code></pre>

<p>You could essentially do the same thing but with a 'username' variable.</p>

<pre><code>'u/(?P&lt;username&gt;[^\/]+)' =&gt; '_thisTemplate',
</code></pre>

<p>You can then use the 'username' variable in your template to retrieve the corresponding user.</p>

<pre><code>{% set user = craft.users.username(username).first() %}
{% if user %}
    {{ user.firstName }}
    ...
{% else %}
    {% redirect 404 %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-06-01T18:06:22.473Z
Note that by ```{% set user %}``` you're overwriting Craft's global ```user``` variable (i.e. _the currently logged in user_). Not a problem in most cases, but probably worth mentioning.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-06-01T18:14:38.023Z
@mmikkel Good point. Although, 'currentUser' would still refer correctly to the currently logged in user, which I believe is the recommended usage anyway.
Douglas McDonald
CC BY-SA 3.0
2015-06-01T20:30:44.193Z
To match everything but a slash for a URL pattern use this regular expression: `(?P<username>[^\/]+)`
carlcs
CC BY-SA 3.0
2015-06-01T21:37:21.597Z
Thanks for the addition @carlcs. You also made me realize that I had an error in my expression, which was still expecting 4 digits. Corrected.
Douglas McDonald
CC BY-SA 3.0
2015-06-02T08:08:36.813Z
For the given example above, How would you construct the link? `<a href="{{ url('u/' ~ entry.author.username) }}">{{ entry.author.username }}</a>` is what I have, but with `404` redirection.  But anyway, some time ago I posted the question, if there would be possible to add a prefix to variable, like popular '#username'. In this case we wouldn't have to worry about any conflicts. Thanks to @Douglas McDonald, @mmikkel and @carlcs on participating this answer.
Dominik Krulak
CC BY-SA 3.0
2015-06-02T16:38:02.833Z
Yes. The link you posted in your comment looks correct. As for the extra character, the pound sign "#", as you probably know, is reserved for anchor tag links — so I would suggest using something else at a minimum, perhaps dash "-" or underscore "_". Although the best in my opinion is still a subpath such as "/u/username".
Douglas McDonald
CC BY-SA 3.0
2015-06-02T20:19:39.923Z
Yes, a subpath is very good insurance. And I think, I leave it like that. Also I've voted to reopen [the question](http://craftcms.stackexchange.com/questions/9909/is-it-achievable-to-route-a-dynamic-uri-with-special-character) as it builds to this comment conversation.
Dominik Krulak
CC BY-SA 3.0
2015-06-01T17:55:00.217Z
2015-06-01T21:34:52.187Z
2015-06-01T21:34:52.187Z
Douglas McDonald
<p>So what I'd like is:</p>

<p>If URI would look like {username}, load _thisTemplate.</p>

<p>Couldn't find the way without solid point like author ID.</p>

CC BY-SA 3.0
2015-06-01T12:53:47.623Z
Had the same issue, had to work around like: `/people/phil-collins/832` ...
tom
CC BY-SA 3.0
2015-06-01T15:31:38.633Z
One of the options, yes. I doubt, that it is possible yet. The craft needs to know, what user is the click link to and without assigning an user's ID, I think it's impossible.
Dominik Krulak
<routing>
Is it possible to route to www.site.com/username?
# Is it possible to route to www.site.com/username?

(Asked by: username_0 on 2015-06-01)

So what I'd like is:

If URI would look like {username}, load \_thisTemplate.

Couldn't find the way without solid point like author ID.

**Tags:** routing
> **Comments:**
> **username_1**: Had the same issue, had to work around like: `/people/phil-collins/832` ...  
> **username_0**: One of the options, yes. I doubt, that it is possible yet. The craft needs to know, what user is the click link to and without assigning an user's ID, I think it's impossible.  

---

## 1 answers

### Answer by username_2 on 2015-06-01. Score: 10 | Accepted answer

I would not particularly advise creating a uri pattern that was just 'www.site.com/username' because usernames can essentially be anything which might cause routing conflicts (i.e. if the username is 'index' for example). You could however use something like 'www.site.com/members/username' or 'www.site.com/u/username'.
One way to do this would be to add a route using a 'subpattern'. For more info check out the routing docs on accessing subpatterns in your templates.
Here is the example given in the docs, which provides the variables 'year' and 'month' to the template that correspond to the matched uri segments (i.e.  `news/2015/02'):
'news/(?P\d{4})/(?P\d{2})' => 'news/_archive',

You could essentially do the same thing but with a 'username' variable.
'u/(?P[^\/]+)' => '_thisTemplate',

You can then use the 'username' variable in your template to retrieve the corresponding user.
{% set user = craft.users.username(username).first() %}
{% if user %}
    {{ user.firstName }}
    ...
{% else %}
    {% redirect 404 %}
{% endif %}

> **Comments:**
> **username_3**: Note that by ```{% set user %}``` you're overwriting Craft's global ```user``` variable (i.e. _the currently logged in user_). Not a problem in most cases, but probably worth mentioning.  
> **username_2**: @mmikkel Good point. Although, 'currentUser' would still refer correctly to the currently logged in user, which I believe is the recommended usage anyway.  
> **username_4**: To match everything but a slash for a URL pattern use this regular expression: `(?P<username>[^\/]+)`  
> **username_2**: Thanks for the addition @carlcs. You also made me realize that I had an error in my expression, which was still expecting 4 digits. Corrected.  
> **username_0**: For the given example above, How would you construct the link? `<a href="{{ url('u/' ~ entry.author.username) }}">{{ entry.author.username }}</a>` is what I have, but with `404` redirection.  But anyway, some time ago I posted the question, if there would be possible to add a prefix to variable, like popular '#username'. In this case we wouldn't have to worry about any conflicts. Thanks to @Douglas McDonald, @mmikkel and @carlcs on participating this answer.  
> **username_2**: Yes. The link you posted in your comment looks correct. As for the extra character, the pound sign "#", as you probably know, is reserved for anchor tag links — so I would suggest using something else at a minimum, perhaps dash "-" or underscore "_". Although the best in my opinion is still a subpath such as "/u/username".  
> **username_0**: Yes, a subpath is very good insurance. And I think, I leave it like that. Also I've voted to reopen [the question](http://craftcms.stackexchange.com/questions/9909/is-it-achievable-to-route-a-dynamic-uri-with-special-character) as it builds to this comment conversation.  

---

