我正在尝试设置 Yii2 以使用漂亮的 URL。为了做到这一点,我配置了 Yii2 并使用 apache 的重写模块来确保我们始终通过入口点(索引)输入.php
我在 yii-application/frontend/web/文件夹中的 .htaccess 文件中进行了以下修改 - 包含索引.php(高级 yii2 模板)的文件夹。对于这些修改,我遵循了各种论坛上的说明。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
我还对yii2的配置进行了以下更改,以激活漂亮的URL
$config = [
'components' => [
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => array(
'<controller:w+>/<id:d+>' => '<controller>/view',
'<controller:w+>/<action:w+>/<id:d+>' => '<controller>/<action>',
'<controller:w+>/<action:w+>' => '<controller>/<action>',
),
],
],
];
请注意,在我进行这些更改之前,Yii2 运行良好。
现在,如果我尝试使用以下 URL 之一进行连接(就像修改之前一样),我会看到登录页面。导航不起作用,我将始终看到登录页面。
http://localhost/frontend/
http://localhost/frontend/index.php
http://localhost/frontend/index.php?r=site/about-us
http://localhost/frontend/index.php?r=site/faq
如果我尝试使用以下 URL 进行连接(一旦正确配置了漂亮的 URL,我应该这样做),我的浏览器会显示一条错误消息。
http://localhost/frontend/site/faq
http://localhost/frontend/site/account
http://localhost/frontend/site/index
错误信息:
Not Found
The requested URL /web/yii-application/frontend/web/index.php was not found on this server.
Apache/2.4.9 (Win32) PHP/5.5.12 Server at localhost Port 80
但是,看起来路径是正确的。index.php 文件实际上位于文件夹 C:\web\yii-application\frontend\web\index 中.php为什么我的浏览器找不到该文件?
感谢您的帮助
在您的所有请求中,您都缺少 Web 文件夹。
您的所有网址都应是
http://localhost/frontend/web/
http://localhost/frontend/web/index.php
http://localhost/frontend/web/index.php?r=site/about-ushttp://localhost/frontend/web/index.php?r=site/faq
http://localhost/frontend/web/site/faq
http://localhost/frontend/web/site/accounthttp://localhost/frontend/web/site/index
除非您设置了文件夹重定向,但考虑到您如何设置,否则我严重怀疑这一点。