如何在Yii中创建自定义网址[复制](How to create custom url in Yii

2019-08-02 02:07发布

可能重复:
如何使类似Facebook的用户配置文件链接,我的网页

如何管理自定义网址像用户配置文件

www.exampl.com/brito  insted of  www.exampl.com/user/profile/id/22
www.exampl.com/jhon   insted of  www.exampl.com/user/profile/id/31
www.exampl.com/piter  insted of  www.exampl.com/user/profile/id/66

我在下面的代码做我的config / main.php文件,现在它的作品这3个静态用户。 我怎么可以动态地改变它为用户的所有1000?

  urlManager'=>array(
                'urlFormat'=>'path',
                'showScriptName'=>false,
                'rules'=>array(
                    '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                    'brito'=>'user/profile/id/22',
                    'john'=>'user/profile/id/31',
                    'piter'=>'user/profile/id/66',
                ),
            ),

请帮我的任何一个。

Answer 1:

htaccess的使用

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# do not do anything
RewriteRule ^ - [L]

# forward /blog/foo to blog.php/foo
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]

# forward /john to user/profile/?name=john
RewriteRule ^((?!blog/).+)$ user/profile/?name=$1 [L,QSA,NC]

并确保ü必须包括name的页面



文章来源: How to create custom url in Yii [duplicate]