删除Yii 2.0中的部分URL

时间:2016-07-26 08:09:47

标签: .htaccess yii2

如果我输入网址:

http://localhost/third/web/calculator

它运作正常。我现在想要从URL中删除web部分。我目前没有.htaccess。如果我输入http://localhost/third/calculator,则表示找不到对象。

我在项目的根文件夹中添加了.htaccess:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/third/(assets|css)
RewriteRule ^assets/(.*)$ web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]

RewriteCond %{REQUEST_URI} !^/third/web/(assets|css)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ web/index.php

现在,如果我输入http://localhost/third/calculator,它会显示yii2的祝贺页面,而不是计算器页面。如果点击任何其他标签,例如使用链接<a href="<?php echo Yii::$app->request->baseUrl;?>/login"><span class="glyphicon glyphicon-log-in"></span> Login</a>登录,则网址仍会显示网络。

2 个答案:

答案 0 :(得分:1)

首先,您更改 config-&gt; web.php

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

在项目根目录中添加 .htaccess 文件

# prevent directory listings
Options -Indexes
IndexIgnore */*

# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^(/.+)?$ web/$1 [L,PT]
RewriteRule ^(.+)?$ web/$1

此外,您必须在 your_project-&gt;网络目录中添加.htaccess个文件

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

答案 1 :(得分:0)

以下是我的设置:

根文件夹中

1) .htaccess

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule (.*) web/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . web/index.php
</IfModule>

2) config / web.php

'components' => [
    'request' => [
        // ...
        'baseUrl' => '' // don't forget about this string
    ],
    // ...
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'enableStrictParsing' => true,
        'rules' => [
            // ...
        ],
    ],
],