隐藏';web';文件夹中的url


problems when hiding the 'web' folder from the url in YII2 basic application

YI2基本应用程序安装在localhost下的"ims"文件夹中。链接就像

http://192.168.0.99/ims/web/(主页)

http://192.168.0.99/ims/web/index.php?r=site%2Fabout(关于我们页面)

到目前为止,我所做的是。

1) 在web/.htaccess文件中

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

2) 在root.htaccess 中

Options -Indexes
RewriteEngine on
RewriteRule ^(.*)$ web/$1 [L]

3) 在config/web.php 中

'components' => [
        'urlManager' => [
            'class' => 'yii'web'UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName'  => false,
            'baseUrl' => '/',
        ],

这修复了以下问题:

1) 链接是SEO友好现在

2) index.php现在不显示在url 中

3) 主页可通过http://192.168.0.99/ims/

问题:-关于,联系&登录链接现在更改为

http://192.168.0.99/site/about

http://192.168.0.99/site/contact

http://192.168.0.99/site/login

它错过了url"ims"中的基本文件夹名称。对此有什么建议或想法吗?

注意:-我不希望使用Apache配置来实现这一点,也不希望将web文件夹的内容移动到外部。我希望在不改变YI2基本应用程序结构的情况下,从url中隐藏"web"。

到目前为止,这对我来说终于奏效了

1.在web/.htaccess文件中

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

2.然后在root.htaccess

<code>
    Options -Indexes
    RewriteEngine on
    RewriteRule ^(.*)$ web/$1 [L]
</code>

3.在config/web.php中

<code>
    use 'yii'web'Request;
    $baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());
    'components' => [
        'request' => ['baseUrl' => $baseUrl,],
        'urlManager' => [
            'class' => 'yii'web'UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName'  => false,
        ],
</code>

注意:只需删除config/web.php 中的"baseUrl"=>"/"

我在这里回答我自己的问题:-

我唯一需要的改变是让它在上工作

'baseUrl' => '/', to 'baseUrl' => '/ims',

所以更改后的代码看起来像

    'components' => [
        'urlManager' => [
            'class' => 'yii'web'UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName'  => false,
            'baseUrl' => '/ims',
        ],

现在我可以浏览所有没有文本"web"的页面。这是在不进行任何apache配置或移动根目录中的web文件夹的情况下实现的。:)

  1. 在web/.htaccess文件中

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
  1. 在root.htaccess中

Options -Indexes
RewriteEngine on
RewriteRule ^(.*)$ web/$1 [L]
  1. 在config/web.php中

use 'yii'web'Request;
$baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());
'components' => [
        'request' => [
            'baseUrl' => $baseUrl,
        ],
        'urlManager' => [
            'class' => 'yii'web'UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName'  => false,
            'baseUrl' => '/',
        ],