我试图在一个Share Hosting环境中部署Yii2(基本模板)的小型项目。一切都在当地正常工作。 主机服务器就像下面的根目录:Yii2 Pretty Url在Share Hosting(基本模板)中不起作用
/
---logs
---index.html
阅读Yii2的doc后。然后删除文件index.html并将我的项目文件夹上传到根目录。它现在看起来像这样:
/
---logs
---assets
---commands
---config
---controllers
--- ...
---views
---web
--- ...
尝试网站后,我有一个空白页。于是我决定创建主机服务器的根目录htaccess文件指向网络文件夹喜欢这里
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
在这之后我能得到的索引页非常好,
www.example.com
和当我尝试访问例如(与URL prettyUrl激活)时,我得到空白页
www.example.com/articles/list
,但是当我停用prettyUrl,它那像工作例如
www.example.com/web/index.php?r=articles/list
在Web文件夹中的.htaccess是这样的:
Options +FollowSymLinks
IndexIgnore */*
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
# Disable Directory Browsing
Options All -Indexes
和URL规则在web.php设置文件如下:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'baseUrl' => '/',
'rules' => [
...
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
我要做的第一件事就是在'.htaccess'文件中引入一些垃圾来检查它是否被读取。如果是这样,访问任何页面时应该弹出一个错误500 – gmc
@gmc我做了它并且读取了.htaccess文件。 – stfsngue