Routing www.domain.com/username

Dear all,

I am very new to the Yii framework and I want to create my first website with it.

A user can register on my website and create a user name. After the registration process he should be able to access his own page(with custom css) with the url:

www.domain.com/username

where "username" stands for his username he inserted in the user registration form. As far as I know I have to add a new route for this after all other routes.

Can somebody let me know how the route for this will be implemented? I am also a little bit concerned that I could create a security issue when I treat them false.

Further I don’t know how to prevent the user don’t type in some keywords like “admin” which I need for my admin module url www.domain.com/admin for his username.

Thank you very much for your help!

This is quite simple, you will need a rule like this in the url manager:




'<username:[a-zA-Z0-9_-]+>'=>'user/profile',

//$_GET['username'] will return the value if it is a match.



But you need to have in mind that, you will have some issues with the other pages of the website, therefore, you need to pass a url rewrite rule for each of them and these rules must come before the above rule, so you would have:




[... all other rules here...]


'<username:[a-zA-Z0-9_-]+>'=>'user/profile',



Now, regarding the prevention of certain names usage, this is also simple,

In the registration process, define a array (or load a database table having forbidden names) and if the username choosen by the user matches one of the forbidden ones, just let the user know this and let him take another username.

Why not read a topic few lines below?

P.S. Welcome to the forum, we have a nice (maybe not the best) search function there ;)

hi, thanks for all your replies. I have now the following routes:

‘<controller:\w+>/<id:\d+>’=>’<controller>/view’,

‘<controller:\w+>/<action:\w+>/<id:\d+>/*’=>’<controller>/<action>’,

‘<controller:\w+>/<action:\w+>’=>’<controller>/<action>’,

‘<page:[a-zA-Z0-9_-]+>’=>‘page/show’,

where the last rule is reponsible to forward www.mydomain.com/test to pass page param to pagecontroller with action show. the problem I now have is that if I go to other urls like www.domain.com/gii he is routing to my pagecontroller. am i doing something wrong here, i thought if i add this as the last route it should pick than the available one.

thanks for your help!!

can nobody help me?

Routes are executed one by one and /gii matches last one. That’s why it’s routing to page/show. To fix it you should add a route for gii at the top:




'gii'=>'gii',



hi twisted,

i use your suggested rule in my application.but it doesn’t work for me .its return me a 400 error message…would you please suggest me something…

If it returns a 404 means that you are doing something wrong.

If for example, you have a rule like :

<username:[a-zA-Z0-9_-]+>’=>'user/profile

And you get a 404 when you go to site.com/myname, what do you get when you go to

mysite.com/user/profile/username/myname ?