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

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

1
2
3
4
5
6
7
8
‘urlManager’ => [
        ‘class’ => ‘yii\\web\\UrlManager’ ,
        // Hide index.php
        ‘showScriptName’ => false ,
        // Use pretty URLs
        ‘enablePrettyUrl’ => true ,
        ‘rules’ => [
        ] ,

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

1
2
3
4
5
6
7
8
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 文件并更改如下:

1
2
3
4
5
<Directory / var /www />
    Options Indexes FollowSymLinks
    AllowOverride All <! instead of none –>
    Require all granted
</Directory >

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

1  grep R AllowOverride /etc /apache2

如下图所示:

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

现在:

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

http://localhost/yii2/web/

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

但是这些 URL 不起作用(发现 404)

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

如何在 Yii2 中使用漂亮的 url 访问控制器

在 Yii2 中启用干净的 URL



相关讨论

  • 在上一步之后,还可以尝试清除浏览器缓存或使用其他浏览器。
  • 谢谢你的回答,它真的帮助了我,但是我在我的 yii 应用程序 web 根目录中只使用了一个 .htaccess,并在我的默认网站配置中添加了 <Directory”varwwwhtml”> AllowOverride All <Directory> 即:文件 “/etc/apache2/sites-available/000 -default.conf” ps:如果使用高级模板,你应该在前端web目录和后端添加.htaccess文件希望这有帮助
  • 这是另一种选择:stackoverflow.com/a/42906056/5247564


终于我成功了:-

1。创建了两个 .htaccess 文件(一个在根目录,一个在我的应用程序的 web 文件夹中):-

root .htaccess:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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:-

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

2。 config/web.php 更改:-

1
2
3
4
5
6
7
‘urlManager’ => [
        ‘enablePrettyUrl’ => true ,
        ‘showScriptName’ => false ,
        ‘rules’ => [
        // Your rules here
        ] ,
    ] ,

3。 apache2.conf 更改:

1
2
3
4
5
<Directory / var /www /html />
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory >

4。现在运行以下命令:-

一个。 sudo /etc/init.d/apache2 stop (停止 apache)

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


你为什么不在你的 web.php 文件中给出规则呢?如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
‘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+>//<id:\\d+>’ => ‘<controller>/’ ,
                ‘<controller:\\w+>/’ => ‘<controller>/’ ,
        ) ,
    ] ,

我在这里设置的规则只是一个例子,你可以按照你想要的方式设置它。

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

1
2
3
4
5
6
7
<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 >



相关讨论

  • 我已经找到了解决方案,但感谢您的无价帮助。 1.


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

1 sudo a2enmod rewrite

然后重新启动 Apache Web 服务器

1 sudo systemctl restart apache2

它应该可以解决问题。


声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。