简体   繁体   中英

I want to use username as a page address in url

For example I want to access username page directly like this:

mysite.com/username

to redirect to my SiteController.

It is named URL rewriting and is provided by the server through some server module. For example mod_rewrite in apache.

Once configured, it will get needed parameters from the URL and send them to your script in an usual manner, through the QUERY_STRING environment variable.

You should not change your php code.

Try this in the .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /SiteControllet.php?id=$1 [L]

This will actually work as
Sitecontroller.php?username=somename You'll need to use user.php to find data.

First of course you'll need to enable mod_rewrite in the apache configuration.

Umcomment this line in the config file:

LoadModule rewrite_module modules/mod_rewrite.s

Remember, the .htaccess should be in the root folder.

Several solutions, but i think there's a more Yii-like way to do it.

First off you need the default .htacces-file in your web folder:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php 

Then you need the urlManager in either your common/config/main or your frontend/common/main.

'components' => [
    'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => [
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            '<username:\w+>' => 'site/profile'  // This will handle your usernames
        ],
    ],
],

And finally a actionProfile($username) in your site controller

public function actionProfile($username) {
    $model = \common\models\User::find()->where(['username' => $username])->one();
    die($model->username);
}

You might also find these links useful:

http://www.yiiframework.com/doc-2.0/yii-behaviors-sluggablebehavior.html http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html

Edit: Oh, wait! You want all the site/actions to work on yoururl.com/actions ?

Then you'll need

'components' => [
        'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => [
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            '<alias:index|all|your|actions|here>' => 'site/<alias>',
        ],
    ],

Or:

'components' => [
        'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => [
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            '<action:\w+' => 'site/<action>',
        ],
    ],

create a file named '.htaccess' and write the following in the file

RewriteEngine On
RewriteCond %{filename} !-d
RewriteCond %{filename} !-f
RewriteRule ^([^/]*)$ /user.php?id=$1 [L]

hope it helps ! :D or for more advanced options you may search for "url-rewriting" .

PS i am not a native english speaker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM