use*_*282 63 clean-urls yii2 yii-url-manager
如何在Yii2中启用干净的URL.我想删除index.php和'?' 来自url参数.需要在Yii2中编辑哪个部分?
use*_*282 152
我在yii2工作了.启用mod_rewrite
了Apache
.要basic template
执行以下操作:在Web文件夹中创建.htaccess文件并添加此文件
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
Run Code Online (Sandbox Code Playgroud)
然后在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>',
),
],
Run Code Online (Sandbox Code Playgroud)
在advanced template
创建.htaccess
文件内部backend/web
和frontend/web
文件夹并在urlManager
里面添加组件的情况下common/config/main.php
小智 13
在您的服务器上启用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
Run Code Online (Sandbox Code Playgroud)
Config文件夹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>',
),
],
Run Code Online (Sandbox Code Playgroud)
pko*_*out 13
对我来说,问题是:
<Directory "/path/to/the/web/directory/">
Options Indexes
FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
Run Code Online (Sandbox Code Playgroud)
Ily*_*rim 11
首先,.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]
Run Code Online (Sandbox Code Playgroud)
.htaccess
使用以下内容在Web文件夹中创建另一个文件:
frontend/web/
添加backend/web/
不要忘记将.htaccess
文件添加到两个Web文件夹:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Run Code Online (Sandbox Code Playgroud)
现在已经完成了.在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;
Run Code Online (Sandbox Code Playgroud)
您的网址将更改为:
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
归档时间: |
|
查看次数: |
83021 次 |
最近记录: |