如何在 Yii2 中启用干净的 url。我想删除 index.php 和 '?' 从 url 参数。为此需要在 Yii2 中编辑哪个部分?
14 回答
我让它在 yii2 中工作。启用. mod_rewrite
_ 执行以下操作:在 web 文件夹中创建一个 .htaccess 文件并添加Apache
它basic template
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
然后在 config 文件夹中,在 web.php 中添加到组件
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
如果在里面advanced template
创建.htaccess
文件backend/web
和frontend/web
文件夹并在里面添加urlManager
组件common/config/main.php
第一个重要的一点是
Module_Rewrite 在您的服务器上启用(LAMP、WAMP、XAMP..etc) 用于在 yii2 框架中进行 URL 重新布线 创建一个 .htaccess 文件并放入 /web 文件夹
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
第二步
配置文件夹common/config/main-local.php
添加到组件数组
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
对我来说,问题是:
- 如上所述,web 文件夹中缺少 .htaccess。
- AllowOverride 指令设置为 None,这会禁用 URL 重写。我将其更改为 All,现在漂亮的 URL 可以很好地工作。
<Directory "/path/to/the/web/directory/">
Options Indexes
FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
首先,.htaccess
在您的 Yii2 项目中创建一个根文件夹,其中包含以下内容:
Options +Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
</IfModule>
# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>
# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]
在您的 Web 文件夹中创建另一个.htaccess
文件,其中包含以下内容:
frontend/web/
添加backend/web/
不要忘记将.htaccess
文件添加到两个 Web 文件夹:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
现在它完成了。更改 Yii2 中的 URL 配置:
<?php
use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'aiJXeUjj8XjKYIG1gurMMnccRWHvURMq',
'baseUrl' => $baseUrl,
],
"urlManager" => [
'baseUrl' => $baseUrl,
'enablePrettyUrl' => true,
'showScriptName' => false,
"rules" => [
"home" => "site/index",
"about-us" => "site/about",
"contact-us" => "site/contact",
]
]
],
];
return $config;
您的网址将更改为:
localhost/yii2project/site/about
=> localhost/yii2project/about-us
localhost/yii2project/site/contact
=> localhost/yii2project/contact-us
localhost/yii2project/site/index
=>localhost/yii2project/home
您可以通过以下方式访问您的管理员
localhost/yii2project/backend/web
只是为了加入这个讨论 - 我刚刚安装了 Yii2,它在 config/web.php 中包含以下注释掉的代码:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [],
],
如果您在接受的答案中添加 .htaccess 文件,那么只需取消上面的注释,漂亮的 URL 就可以工作(我不知道接受的答案中的“规则”是什么,但没有它们似乎一切都可以工作)。
在 nginx 上这样配置
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
步骤1:将.htaccess
文件放入根目录。
Options –Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
</IfModule>
# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>
# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]
第二步:把.htaccess
文件放进去frontend/web
。
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
第 3 步:然后更改frontend/config/main.php
. 需要在里面添加以下代码'components' => []
。
'request' => [
'csrfParam' => '_csrf-frontend',
'baseUrl' => '/yii-advanced', //http://localhost/yii-advanced
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false, // Disable index.php
'enablePrettyUrl' => true, // Disable r= routes
'rules' => array(
'about' => 'site/about',
'service' => 'site/service',
'contact' => 'site/contact',
'signup' => 'site/signup',
'login' => 'site/login',
),
],
以上步骤对我有用。
只需将以下代码添加到您的配置文件中。
'urlManager' => [
'enablePrettyUrl' => true,
'rules' => [
// your rules go here
],
// ...
]
分步说明
步骤1
在项目的根目录下添加一个 .htaccess 文件,内容如下:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
RewriteRule ^js/(.*)$ web/js/$1 [L]
RewriteRule ^images/(.*)$ web/images/$1 [L]
RewriteRule (.*) /web/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php
第2步
在文件夹 /web 中添加一个包含以下内容的 .htaccess 文件:
RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
第 3 步
在数组元素组件的文件 /config/web.php 中添加以下代码:
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'yYy4YYYX8lYyYyQOl8vOcO6ROo7i8twO',
'baseUrl' => ''
],
//...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index',
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
],
],
完毕..
我安装了这个框架的新版本。
在backend/config/main.php
中,您可以看到注释的代码,您可以使用它并对文件夹执行此frontend
操作。
Step1:在项目config/main.php 例如:frontend/config/main.php
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [],
]
Step2: 创建.htaccess 文件 inset web 文件夹 eg: frontend/web
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
#php_flag display_errors on
#php_value error_reporting 2039
什么对我有用-
在我的 Yii2 项目的根文件夹中创建一个 .htaccess,并添加了以下内容-
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/.*
RewriteRule ^(.*)$ web/$1 [L]
RewriteCond %{REQUEST_URI} !^/web/
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ web/index.php
</IfModule>
创建了具有以下内容的新 .htaccess 文件网络文件夹:
frontend/web/
并添加了以下内容-
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
然后在此处添加 urlmanager-
projectFolder/common/config/main.php
对我来说它不存在,所以添加了这个 -
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
/* 'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],*/
],
确保此代码必须在'components' => []
.
重新启动我的服务器,一切正常。
如果您已安装 yii2 应用程序主题
,请转到 basic/web/
inside -> .htaccess "如果不存在则粘贴以下代码"
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
然后转到
web.php 内的 config/取消注释从 47 到 52 的行(可能会更改行)或类似的东西..
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
配置/web.php
$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
'components' => [
'assetManager' => [
// override bundles to use local project files :
'bundles' => [
'yii\bootstrap4\BootstrapAsset' => [
'sourcePath' => '@app/assets/source/bootstrap/dist',
'css' => [
YII_ENV_DEV ? 'css/bootstrap.css' : 'css/bootstrap.min.css',
],
],
'yii\bootstrap4\BootstrapPluginAsset' => [
'sourcePath' => '@app/assets/source/bootstrap/dist',
'js' => [
YII_ENV_DEV ? 'js/bootstrap.js' : 'js/bootstrap.min.js',
]
],
],
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'V_Pj-uMLTPPxv0Be5Bwe3-UCC6EjGRuH',
'baseUrl' => '',
],
'formatter' => [
'dateFormat' => 'dd/MM/yyyy',
'decimalSeparator' => ',',
'thousandSeparator' => '.',
'currencyCode' => 'BRL',
'locale' => 'pt-BR',
'defaultTimeZone' => 'America/Sao_Paulo',
'class' => 'yii\i18n\Formatter',
],
'datehelper' => [
'class' => 'app\components\DateBRHelper',
],
'formatcurrency' => [
'class' => 'app\components\FormatCurrency',
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '123456',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
'' => 'site/index',
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
],
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
// uncomment the following to add your IP if you are not connecting from localhost.
//'allowedIPs' => ['127.0.0.1', '::1'],
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
// uncomment the following to add your IP if you are not connecting from localhost.
//'allowedIPs' => ['127.0.0.1', '::1'],
];
}
return $config;
arquivo .htaccess na Pasta raiz
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/.*
RewriteRule ^(.*)$ web/$1 [L]
RewriteCond %{REQUEST_URI} !^/web/
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ web/index.php
</IfModule>
.htaccess 意大利面食web/
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php