Profile URL

What are the rules for rewriting all urls to a controller/action? I am trying

‘’=>‘site/index’,

‘<profile>’ => ‘site/users/show?id=<profile>’,

I want something like www.site.com/memberName to redirect to the users/show action, passing a get var from the end of the url. Thanks.

Try this:




'rules'=>array(

  ...

  '<profile:[^/]+>'=>'users/show',

),



I’m assuming you’ve got a controller named UsersController with a method UsersController.actionShow($profile). It won’t work otherwise.

Thanks, but is that a valid regular expression? Yii is saying it isn’t,

Similar to the Yii’s default rules:




...

'rules'=>array(

...

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

...

),



In your case, you could try:




'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',



Thanks. I tried:




'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',



and




'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',



and




'/<id:\d+>' => 'user/show',



With no luck. That last one is what I think is most representative of what I’m trying to do, which is to redirect www.domain.com/profileName to the user/show action and pass the id via GET. The equivalent name should be something like www.domain.com/user/show/id/profileName but I’m just trying to shorten it to www.domain.com/profileName.

I’m building a really user heavy site, and we need to shorten up the url to something like domain.com/joe or domain.com/me

I added the rewrite rules to remove index.php. Here is my .htaccess:




RewriteEngine on


#RedirectMatch (.*)admin\.php$ $1backend.php

#RedirectMatch (.*)admin$ $1backend.php

#RedirectMatch (.*)admin/$ $1backend.php


# if a directory or a file exists, use it directly

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d


#otherwise forward it to index.php

RewriteRule . index.php


<IfModule mod_headers.c>

 <FilesMatch "\.(swf|gif|jpg|png|ico|cur|css|js)$">

  Header set Cache-Control "max-age=604800"

 </FilesMatch>

</IfModule>


<IfModule mod_expires.c>

 ExpiresActive On

 ExpiresDefault A604800

 ExpiresByType application/x-shockwave-flash A604800

 ExpiresByType image/gif A604800

 ExpiresByType image/jpeg A604800

 ExpiresByType image/png A604800

 ExpiresByType image/x-icon A604800

 ExpiresByType image/x-win-bitmap A604800

 ExpiresByType text/css A604800

 ExpiresByType text/javascript A604800

</IfModule>


RewriteEngine on



Thanks!

Ganh. For real? It certainly is: It should match all characters except the backslash. Try \w+ instead.

If I got you right,

you are trying to redirect

www.domain.com/user/show/id/profileName to www.domain.com/profileName.

In this case, UserController and actionShow is considered.

If the data of profileName comes from db, you may have to get it manually from model, (but if it stores in Session then it can be used directly) and place it directly in the rule as a php variable which has its proper profileName value.

Then it should be:





'<controller:\w+>/<action:\w+>/<id:\d+>/<profile:\w+>' =>'/'.$profileName,




If the profileName has value then it should work. Just try it but i am not sure.

Hope that helps :)

To give everyone an update, all methods throw a 404 error inside the Yii template when you try www.mydomain.com/profileName. The homepage works on / and actions work on controller/action url.

I am going to try to use straight .htaccess. Would I have to change the config file to show index.php again and rewrite the .htaccess rules to redirect everything to index.php?r=user/show&id=profileName? Thanks.

This worked for me:

http://www.yiiframework.com/forum/index.php?/topic/10829-need-of-url-with-username-similar-to-facebook/page__view__findpost__p__53282