我使用urlManager配置来获取这样的漂亮网址:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'login/' => 'site/login',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>/',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>/',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
],
],
它正在我的本地主机上工作但不在服务器上工作。 .htaccess文件内容:
# Increase cookie security
<IfModule php5_module>
php_value session.cookie_httponly true
</IfModule>
# Settings to hide index.php and ensure pretty urls
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
答案 0 :(得分:1)
要做的重要事情:
Recent Apache versions (from 2.3.9) have "AllowOverride None" by default and versions before had "AllowOverride All" (see allowoverride).
因此代码可以在localhost上运行,但由于apache版本不同而在服务器上提供404。
http://www.yiiframework.com/forum/index.php/topic/51470-pretty-urls-and-htaccess
答案 1 :(得分:1)
在YII2 Advance模板中启用Pretty URL。
首先在项目目录的根目录下创建一个.htaccess文件,就像你的项目目录名是“Yii-Advance”一样,然后在那里创建一个文件Yii-Advance / .htaccess和putt。
第1步
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/(admin)
RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [L]
RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/
RewriteCond %{REQUEST_URI} ^/(admin)
RewriteRule ^.*$ backend/web/index.php [L]
RewriteCond %{REQUEST_URI} ^/(assets|css)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php
</IfModule>
第2步
创建一个名为“Request.php”的新文件,并使用此代码将其放在“common / components /”下。
<?php
namespace common\components;
class Request extends \yii\web\Request {
public $web;
public $adminUrl;
public function getBaseUrl(){
return str_replace($this->web, "", parent::getBaseUrl()) . $this->adminUrl;
}
/*
If you don't have this function, the admin site will 404 if you leave off
the trailing slash.
E.g.:
Wouldn't work:
site.com/admin
Would work:
site.com/admin/
Using this function, both will work.
*/
public function resolvePathInfo(){
if($this->getUrl() === $this->adminUrl){
return "";
}else{
return parent::resolvePathInfo();
}
}
}
第3步
现在安装将这些行放在frontend / config / main.php
下 'request'=>[
'class' => 'common\components\Request',
'web'=> '/frontend/web'
],
第4步
将这些行放在backend / config / main.php
下 'request'=>[
'class' => 'common\components\Request',
'web'=> '/backend/web',
'adminUrl' => '/admin'
],
第6步
以这种方式在Frontend / config / main.php和backend / config / main.php中启用漂亮的URL,在我的情况下我有两个控制器“Site,User”
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<alias:index|login|logout|contact|about|signup|request-password-reset|reset-password>' => 'site/<alias>',
'<alias:index|create|confirm|confirmation|update|delete>' => 'user/<alias>',
],
],
就是这样
现在,当您访问yii2高级模板应用程序时,您将以这种方式看到网址
在执行这些步骤之前,网址为:http://localhost/Yii-Advance/frontend/web/site/login
完成以下步骤后,网址为:http://localhost/Yii-Advance/login