Yii2从网址中删除index.php

iszxjhcz  于 2022-11-09  发布在  PHP
关注(0)|答案(5)|浏览(170)

当我在Yii中设置从URL中删除index.php的选项时,我得到了一个404错误,这个错误记录在错误日志File does not exist: /var/live/var中。在我的浏览器中,我得到了这个错误The requested URL /var/decat/frontend/web/index.php was not found on this server.,但是文件正好在那个位置。可能的解释是我的文档根目录是/var/live,decat是conf文件中显示的别名。
这个url在http://13.21.16.180/decat/index.php/site/login上运行得很好,但是当我删除index.php时,我会收到错误。我按照所有的说明在conf文件中设置它。我甚至尝试通过一个.htaccess文件。下面是我的conf文件中的信息。

Alias /decat /var/decat/frontend/web

<Directory "/var/decat/frontend/web">
        # use mod_rewrite for pretty URL support
        RewriteEngine on
        # If a directory or a file exists, use the request directly
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        # Otherwise forward the request to index.php
        RewriteRule . index.php 
        Options -Indexes FollowSymLinks 
        AllowOverride All
        Order allow,deny 
        Allow from all 
</Directory>
gg58donl

gg58donl1#

RewriteEngine on之后添加RewriteBase /decat/,然后重新启动apache。

t9aqgxwy

t9aqgxwy2#

应按如下方式设置Url Manager组件:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false, // Only considered when enablePrettyUrl is set to true
],

正式文件:

  • $启用漂亮URL
  • $显示脚本名称
wmtdaxz3

wmtdaxz33#

您应该将.htaccess更改为

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php?r=$1 [L,QSA]

和urlManager规则为

'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>',
    ),
    ],

我在我的应用程序中使用这个。它工作。我的网址鞋作为www.example.com/user/view?id=1

rmbxnbpk

rmbxnbpk4#

yii 2从url中删除网页

1)使用以下命令编辑您的config/web.php文件:

<?php
use \yii\web\Request;

$baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());

return [
    ...
    'components' => [
            'request' => [
                'baseUrl' => $baseUrl,
     ],
      ...
    ]
]
?>

2)将以下htaccess添加到root/web

RewriteEngine On RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

3)将以下htaccess添加到安装应用程序的根文件夹


# prevent directory listings

Options -Indexes
IndexIgnore */*

# follow symbolic links

Options FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)?$ web/$1

pprl5pva

pprl5pva5#

我想是你的Apache配置错了......你告诉它在我阅读的时候只重写一个字符的请求?
我认为它需要:

RewriteRule ^/(.*) index.php/$1 [L]

但我可能会错,不是一个apacheMaven

相关问题