我正在完成我在Yii应用程序上的工作,我想拥有更漂亮的URL。我通过谷歌搜索网站和Yii维基,并根据手册进行。但是,我陷入了无法隐藏索引.php形成我的 URL 的地步。使用索引.php在我的 url 中它可以工作,网站打开,没有它,它会让我使用 服务器 404。
这是我的主配置:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<action>'=>'site/<action>',
'<controller:w+>/<id:d+>' => '<controller>/view',
'<controller:w+>/<action:w+>/<id:d+>' => '<controller>/<action>',
'<controller:w+>/<action:w+>' => '<controller>/<action>',
),
),
将"showScriptName"设置为false时,我得到404。
这是我的.htaccess域名
RewriteEngine on
RewriteBase /
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php)
# otherwise forward it to index.php
RewriteRule ^(.+)$ index.php?$1 [PT,L,QSA]
我目前正在使用 WAMP 在本地主机上测试该站点。Apache上的Mod_rewrite正在运行。将 http.conf 更改为选项索引 followSymLinks 允许覆盖所有。
我还错过了什么?
对于那些挣扎的人!将 .htaccess 放在应用程序的根目录,而不是受保护的文件夹中!!
尝试添加:
'' => 'site/index'
到你的网址管理器"规则"数组。逻辑是,Yii 无法将模式''
的请求与您定义的任何规则相匹配。
'urlManager' => array(
'urlFormat' => 'path',
'urlSuffix' => '.html',
'showScriptName' => false,
// 'caseSensitive'=>false,
'rules' => array(
'<action>'=>'site/<action>',
'<controller:w+>/<id:d+>' => '<controller>/view',
'<controller:w+>/<action:w+>/<id:d+>' => '<controller>/<action>',
'<controller:w+>/<action:w+>' => '<controller>/<action>',
),
),
在您的 .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
在项目文件夹中上传您的 htaccess。
情况下,使用相同的设置,会出现这两个场景:
- https://somedomain.com/action123 [失败,愚蠢的404]
- https://somedomain.com/action123/[成功]
问题是,最后的"/"符号(需要更多规则)