Yii2:-漂亮的 URL 已形成,但不起作用(说 404 NOT FOUND) [英] Yii2:-Pretty URL's are formed, but not working (says 404 NOT FOUND)
问题描述
我已经开始学习 yii2 并且我尝试做漂亮的 URL 内容,但失败了.我做了什么:-
在 config/web.php(我在下面编辑过):
'urlManager' =>['类' =>'yii\web\UrlManager',//隐藏 index.php'showScriptName' =>错误的,//使用漂亮的 URL'enablePrettyUrl' =>真的,'规则' =>[],
然后我创建了一个 .htaccess
文件并将其放在根目录下(它有以下代码):
RewriteEngine on# 如果目录或文件存在,直接使用RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d# 否则将其转发到 index.php重写规则.索引.php
我也打开了 apache2.conf 文件并像这样更改:
<目录/var/www/>期权索引 FollowSymLinks允许覆盖所有 <!-- 而不是没有 -->要求所有授予</目录>
我还通过命令检查了更改:
grep -R AllowOverride/etc/apache2
它显示如下:
/etc/apache2/apache2.conf: AllowOverride All <!-- 显示已完成 -->
现在:
当我通过以下方式访问我的页面时:
它已打开,当我将鼠标悬停在页面的任何链接上时,它向我显示了如下内容:http://localhost/yii2/web/site/about(显示那个漂亮网址的女仆)
但是这些 URL 不起作用(说找到了 404)
我也尝试过下面的帖子代码,但对我不起作用:
最后我成功了:-
1. 创建了两个 .htaccess 文件(一个在我的应用程序的根目录中,一个在我的应用程序的 web 文件夹中):-
root .htaccess:-
选项 +FollowSymlinks重写引擎开启</IfModule><IfModule mod_rewrite.c>RewriteCond %{REQUEST_URI} ^/.*重写规则 ^(.*)$ web/$1 [L]RewriteCond %{REQUEST_URI} !^/web/RewriteCond %{REQUEST_FILENAME} !-f [OR]RewriteCond %{REQUEST_FILENAME} !-d重写规则 ^.*$ web/index.php</IfModule>
web 文件夹 .htaccess:-
RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则.索引.php
2. config/web.php 更改:-
'urlManager' =>['enablePrettyUrl' =>真的,'showScriptName' =>错误的,'规则' =>[//你的规则在这里],],
3. apache2.conf 更改:
<目录/var/www/html/>期权索引 FollowSymLinks允许覆盖所有要求所有授予</目录>
4. 现在运行以下命令:-
a. sudo/etc/init.d/apache2 stop
(停止apache)>
b. sudo killall apache2
(杀死进程)
c. sudo netstat -l|grep www
(检查端口 80 未使用)
d. sudo/etc/init.d/apache2 restart
(重启apache)
现在一切正常.
我真诚地感谢所有试图帮助我的人.
参考:-
https://www.my-yii.com/forum/topic/how-to-set-up-pretty-urls-in-yii2-basic-template
https://askubuntu.com/questions/48362/how-to-enable-mod-rewrite-in-apache
I have started learning yii2 and I tried to do pretty URL stuff, but failed. What I did:-
in config/web.php (I have edited below):
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Hide index.php
'showScriptName' => false,
// Use pretty URLs
'enablePrettyUrl' => true,
'rules' => [
],
then I have created a .htaccess
file and put it on root (it has below code):
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
Also I had opened apache2.conf file and changed like this:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All <! -- instead of none -->
Require all granted
</Directory>
Also I checked the changes through the command:
grep -R AllowOverride /etc/apache2
And it shows like below:
/etc/apache2/apache2.conf: AllowOverride All <!-- It is showing that done -->
Now:
when I access my page through:
it's opened and when I hover on any link of the page,it showed me something like this: http://localhost/yii2/web/site/about (which shows that pretty URL's maid)
But these URL's are not working (says 404 found)
I have tried below posts code also, but didn't worked for me:
How to access a controller with pretty url in Yii2
Finally I made it working:-
1. created two .htaccess file (one on root and one in web folder of my application):-
root .htaccess:-
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/.*
RewriteRule ^(.*)$ web/$1 [L]
RewriteCond %{REQUEST_URI} !^/web/
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ web/index.php
</IfModule>
web folder .htaccess:-
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
2. config/web.php changes:-
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
// Your rules here
],
],
3. apache2.conf changes:
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
4. Now run below commands:-
a. sudo /etc/init.d/apache2 stop
(to stop apache)
b. sudo killall apache2
(to kill process)
c. sudo netstat -l|grep www
(to check port 80 is not in use)
d. sudo /etc/init.d/apache2 restart
(restart apache)
And now everything worked fine.
My sincere Thanks to every-one who tried to help me out.
Reference taken:-
https://www.my-yii.com/forum/topic/how-to-set-up-pretty-urls-in-yii2-basic-template
https://askubuntu.com/questions/48362/how-to-enable-mod-rewrite-in-apache
这篇关于Yii2:-漂亮的 URL 已形成,但不起作用(说 404 NOT FOUND)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!