我想在 url 中使用用户名作为页面地址

I want to use username as a page address in url

例如我想像这样直接访问用户名页面:

mysite.com/username

重定向到我的 SiteController。

它被命名为URL rewriting,由服务器通过一些服务器模块提供。例如 mod_rewrite 在 apache 中。

配置后,它将从 URL 获取所需的参数,并通过 QUERY_STRING 环境变量以通常的方式将它们发送到您的脚本。

您不应更改 php 代码。

在 .htaccess 中试试这个

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

这实际上可以作为
Sitecontroller.php?username=somename 您需要使用 user.php 来查找数据。

首先你当然需要启用 mod_rewrite 在 apache 配置中。

在配置文件中注释掉这一行:

LoadModule rewrite_module modules/mod_rewrite.s

记住,.htaccess 应该在根文件夹中。

创建一个名为“.htaccess”的文件并在文件中写入以下内容

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

希望对您有所帮助! :D 或者您可以搜索 "url-rewriting" .

以获得更高级的选项

P.S。我的母语不是英语。

几个解决方案,但我认为有一个更像 Yii 的方法。

首先,您的网络文件夹中需要默​​认的 .htacces 文件:

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

那么您需要在 common/config/main 或 frontend/common/main 中使用 urlManager。

'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
        ],
    ],
],

最后是您站点控制器中的 actionProfile($username)

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

您可能还会发现这些链接有用:

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

编辑: 等一下!您希望所有 site/actions 都在您的网址上工作。com/actions ?

那么你需要

'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>',
        ],
    ],

或者:

'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>',
        ],
    ],