.htaccess Yii 2:-漂亮的网址已经形成,但不工作(说404 NOT FOUND)

jq6vz3qz  于 2022-11-16  发布在  其他
关注(0)|答案(3)|浏览(120)

我已经开始学习yii 2,我试图做漂亮的网址的东西,但失败了。我做了什么:-

在config/web.php中(我在下面编辑过):

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

然后我创建了一个.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

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

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All <! -- instead of none -->
    Require all granted
</Directory>

我还通过以下命令检查了更改:

grep -R AllowOverride /etc/apache2

显示如下:

/etc/apache2/apache2.conf:  AllowOverride All  <!-- It is showing that done -->

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

http://localhost/yii2/web/
当我将鼠标悬停在页面的任何链接上时,它会显示如下内容:http://localhost/yii2/web/site/about(显示漂亮URL的女仆)
但是这些URL不起作用(显示找到404)

我也试过下面的帖子代码,但对我不起作用:

How to access a controller with pretty url in Yii2
Enable clean URL in Yii2

2nc8po8w

2nc8po8w1#

  • 我终于成功了 *
    ***1.***创建了两个.htaccess文件(一个在根目录上,另一个在应用程序Web文件夹中):-
  • 根目录.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文件夹.htaccess:-*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

***2.***config/web.php文件中进行了以下更改:-

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

***3.***需要在apache2.conf文件中进行更改:

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

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

kb5ga3dv

kb5ga3dv2#

为什么不直接在web.php文件中给予规则呢?

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),
    ],

我在这里设置的规则只是一个例子,你可以设置它的方式,你希望你的网址看起来像。

**EDIT:**如果仍无法正常工作,请尝试使用以下命令设置虚拟主机:

<Directory "/var/www/html/yii2/web/">
    DirectoryIndex index.php
    AllowOverride All
    Order allow,deny
    Allow from all
    Allow from 127.0.0.1 localhost
</Directory>
w8ntj3qf

w8ntj3qf3#

如果以上答案都不起作用,并且您正在使用Apache作为Linux上的Web服务器,那么请确保在Apache中启用了重写模式,如果没有,您可以使用以下命令启用它:

sudo a2enmod rewrite

然后重新启动Apache Web服务器

sudo systemctl restart apache2

它应该能解决问题。

相关问题