Yii2:-形成了漂亮的URL,但是不起作用(说404“找不到”)。 [英] Yii2:-Pretty URL's are formed, but not working (says 404 "NOT FOUND")

查看:481
本文介绍了Yii2:-形成了漂亮的URL,但是不起作用(说404“找不到”)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习yii2,并且尝试做一些漂亮的URL东西,但是失败了。我所做的:-

I have started learning yii2 and I tried to do pretty URL stuff, but failed. What I did:-

在config / web.php中(我在下面进行了编辑):

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Hide index.php
        'showScriptName' => false,
        // Use pretty URLs
        'enablePrettyUrl' => true,
        'rules' => [
        ],

然后我创建了 .htaccess 文件并将其放在根目录(具有以下代码):

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

我还打开了apache2.conf文件并进行了如下更改:

<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 -->

现在:

当我通过以下方式访问我的页面时:

when I access my page through:

http:// localhost / yii2 / web /

它已打开,当我将鼠标悬停在任何链接上时在页面上,它显示了如下内容: http:// localhost / yii2 / web / site / about (显示漂亮的URL的女仆)

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)

但是这些URL无效(表示找到404)

But these URL's are not working (says 404 found)

我也在下面的帖子代码中尝试过,但对我没有用:

I have tried below posts code also, but didn't worked for me:

如何在Yii2中访问具有漂亮url的控制器

在Yii2中启用清理URL

推荐答案

最后,我成功了:-

Finally I made it working:-

< Strong> 1。创建了两个.htaccess文件(一个位于我的应用程序的根目录上,另一个位于我的应用程序的Web文件夹中):-

1. created two .htaccess file (one on root and one in web folder of my application):-

root .htaccess:-

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> 

网络文件夹.htaccess:-

web folder .htaccess:-

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

2。在config / web.php中:-

2.In config/web.php:-

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
        // Your rules here
        ],
    ],

3。在(apache2.conf)中进行了如下更改:-

3. Changed in (apache2.conf) like below:-

<Directory /var/www/html/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

4。然后运行以下命令:-

4.Then run below commands:-

a。 sudo /etc/init.d/apache2停止 (停止Apache)

b。 sudo killall apache2 (杀死进程并检查是否没有进程在运行)

c。 sudo netstat -l | grep www (以检查端口80是否当前未使用)

d。 sudo /etc/init.d/apache2重新启动 (重新启动apache)

现在一切正常。

And now everything worked fine.

顺便感谢所有试图帮助我的人。

By the Way Thanks to every-one who tried to help me out.

参考:-

https://www.my-yii.com/for um / topic / how-to-to-set-up-pretty-urls-in-yii2-basic-template

https://askubuntu.com/questions/48362/how-to-enable-mod-rewrite-in-apache

这篇关于Yii2:-形成了漂亮的URL,但是不起作用(说404“找不到”)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆