Yii2 htaccess - 如何完全隐藏 frontend/web 和 backend/web

Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY

我觉得我很接近。我将 htaccess 重定向到网站 (frontend/web) 和 /admin 路径 (backend/web)。该网站显示正常,CSS 文件加载等

如果您转到:http://localhost/yii2app/ - 它会加载主页,并且不会在地址栏中重定向,但页面在所有 URL 中显示 frontend/web。

如果您转到:http://localhost/yii2app/admin - 它会加载后端登录页面,但它会立即重定向到地址栏中的 /backend/web/site/login(丑陋)。

问题:frontend/backend 路径显示在 URL 中(地址栏和页面上的链接)。

我需要什么:我希望整个网站在不显示 frontend/backend 链接的情况下运行。该项目的根应该从 frontend/web 中(不可见地)拉出而不显示它。所以 http://localhost/yii2app/ 运行我的整个前端,并且 http://localhost/yii2app/admin/ 运行我的整个后端。

为什么?我觉得这个设置在服务器上运行时会非常可靠和优雅。我希望能够将我的项目文件夹实时推送到一个站点,并且它工作得很好,而无需 hack 来处理本地与服务器。

.htaccess 文件在 /yii2app 目录中:

Options -Indexes
RewriteEngine on

<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/
    RewriteCond %{REQUEST_URI} admin
    RewriteRule .* backend/web/index.php [L]

    RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/
    RewriteCond %{REQUEST_URI} !admin
    RewriteRule .* frontend/web/index.php [L]
</IfModule>

现在在前端和后端的网站目录中,它们都具有相同的.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

我不想再看到 /frontend/web/backend/web :)

我尝试在 root 的 htaccess 中使用 RewriteRule 将 /admin 添加到 URL,但它一直告诉我 /admin 不存在。我知道它不存在,我不希望它存在。我希望它是一个相对路径..即:/admin == /backend/web.

换一种说法。我将项目根目录 (http://localhost/yii2app/) 中的所有内容加载 frontend/web,但不显示它。此外,http://localhost/yii2app/admin 加载 backend/web 并仅显示 http://localhost/yii2app/admin。显然,他们会附加各自的 controller/action。所以管理员可能看起来像 http://localhost/yii2app/admin/site/login

注意:我没有玩过任何文件。这是一个常用的 yii2 高级设置,使用 composer 并遵循文档。到目前为止,我唯一玩过的就是提到的 htaccess 文件。

谢谢!

如果您的唯一目标是实现永远不会看到 /frontend/web/backend/web,即使不使用 .htaccess 规则,您也可以执行以下操作:

为什么不直接提取 web 文件夹的内容并将它们放在根目录中?调整入口脚本中框架和配置文件的路径即可index.php.

您的目录结构如下所示:

- yii2app/
    - frontend/
    - backend/
    - common/
    - .. other folders..
    - admin/
        - assets/
        - css/
        - index.php
    - assets/
    - css/
    - index.php

你的 yii2app/index.php 看起来像:

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/common/config/bootstrap.php');
require(__DIR__ . '/frontend/config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/common/config/main.php'),
    require(__DIR__ . '/common/config/main-local.php'),
    require(__DIR__ . '/frontend/config/main.php'),
    require(__DIR__ . '/frontend/config/main-local.php')
);

$application = new yii\web\Application($config);
$application->run();

你的 yii2app/admin/index.php 看起来像:

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../common/config/bootstrap.php');
require(__DIR__ . '/../backend/config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../common/config/main.php'),
    require(__DIR__ . '/../common/config/main-local.php'),
    require(__DIR__ . '/../backend/config/main.php'),
    require(__DIR__ . '/../backend/config/main-local.php')
);

$application = new yii\web\Application($config);
$application->run();

编辑:您的入口脚本可能看起来与我的不同,但您应该了解更改路径以使用这些示例查找框架文件。

在寻找最佳解决方案 2 天后,我就这样做了(托管在共享主机上)。 我创建子域 admin.xxxxx.com 并将其指向文档根目录 /public_html/xxxxxx/backend/web
因为共享主机不允许你为你的主域放置自定义文档根目录,所以我使用这个解决方案:

Change the primary domain name on your account, then add the old primary domain as an addon domain. This way, you can pick the root folder you want for the primary domain/new addon domain. (The new primary domain will now point to public_html.)

然后将(现在是我的插件域)指向前端的正确文档根目录 /public_html/xxxxxx/frontend/web

尝试使用 .htaccess 方法 -

步骤 1

在根文件夹中创建 .htaccess 文件,即 advanced/.htaccess 并编写以下代码。

Options +FollowSymlinks
RewriteEngine On

# deal with admin first
RewriteCond %{REQUEST_URI} ^/(admin) <------
RewriteRule ^admin/assets/(.*)$ backend/web/assets/ [L]
RewriteRule ^admin/css/(.*)$ backend/web/css/ [L]

RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/  <------
RewriteCond %{REQUEST_URI} ^/(admin)  <------
RewriteRule ^.*$ backend/web/index.php [L]


RewriteCond %{REQUEST_URI} ^/(assets|css)  <------
RewriteRule ^assets/(.*)$ frontend/web/assets/ [L]
RewriteRule ^css/(.*)$ frontend/web/css/ [L]
RewriteRule ^js/(.*)$ frontend/web/js/ [L] 
RewriteRule ^images/(.*)$ frontend/web/images/ [L]

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

注意 :如果您在本地服务器中尝试,请在看到箭头符号的地方将 ^/ 替换为 ^/project_name/。设置完成后删除那些箭头符号 <------

步骤 2

现在在公共目录中创建一个components/Request.php文件,并在该文件中写入以下代码。

namespace common\components;


class Request extends \yii\web\Request {
    public $web;
    public $adminUrl;

    public function getBaseUrl(){
        return str_replace($this->web, "", parent::getBaseUrl()) . $this->adminUrl;
    }


    /*
        If you don't have this function, the admin site will 404 if you leave off 
        the trailing slash.

        E.g.:

        Wouldn't work:
        site.com/admin

        Would work:
        site.com/admin/

        Using this function, both will work.
    */
    public function resolvePathInfo(){
        if($this->getUrl() === $this->adminUrl){
            return "";
        }else{
            return parent::resolvePathInfo();
        }
    }
}

步骤 3

正在安装组件。分别在 frontend/config/main.phpbackend/config/main.php 文件中写入以下代码。

//frontend, under components array
'request'=>[
    'class' => 'common\components\Request',
    'web'=> '/frontend/web'
],
'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
],

// backend, under components array
'request'=>[
    'class' => 'common\components\Request',
    'web'=> '/backend/web',
    'adminUrl' => '/admin'
],
'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
],

第 4 步(可选,如果到第 3 步还不行)

在 web 目录中创建 .htaccess 文件

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php?/ [L]

注意:确保您已在 apache

中启用 mod 重写

就是这样!您可以使用
尝试您的项目 www.project.com/adminwww.project.com

在本地服务器中
localhost/project_name/adminlocalhost/project_name

我遵循了 "deacs" 的答案,但得到了以下错误

Invalid Configuration – yii\base\InvalidConfigException
The directory does not exist: D:/wamp/www/yii2app/assets

然后我在 "yii2app" 中创建了 "assets" 文件夹并且它有效

------------第二种方法------------------------

在不移动文件的情况下,您可以关注以下内容link

https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/start-installation.md

------------第三种方法-----------------------------

http://www.yiiframework.com/doc-2.0/guide-tutorial-shared-hosting.html

就像@deacs 说的那样,只需将文件从 frontend/web 移动到 yii2app(根文件夹)并在 yii2app "admin" 中创建一个文件夹并将文件从 backend/web 移动到 yii2app/admin 然后使用以下代码在 admin 和 yii2app 中创建 .htaccess 文件:

Options +FollowSymLinks
IndexIgnore */*

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

然后 add/modify 配置文件 main.php 中的 urlManager 组件在 frontend/config/main.php 和 backend/config/main.php 中使用以下代码:

    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'enableStrictParsing' => false,
        'rules' => [
        ],
    ],

然后在 yii2app 中修改 index.php 代码如下:

<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/common/config/bootstrap.php');
require(__DIR__ . '/frontend/config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/common/config/main.php'),
    require(__DIR__ . '/common/config/main-local.php'),
    require(__DIR__ . '/frontend/config/main.php'),
    require(__DIR__ . '/frontend/config/main-local.php')
);

$application = new yii\web\Application($config);
$application->run();

同时使用以下代码更改 yii2app/admin 中的 index.php :

<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../common/config/bootstrap.php');
require(__DIR__ . '/../backend/config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../common/config/main.php'),
    require(__DIR__ . '/../common/config/main-local.php'),
    require(__DIR__ . '/../backend/config/main.php'),
    require(__DIR__ . '/../backend/config/main-local.php')
);

$application = new yii\web\Application($config);
$application->run();

这就是您在 yii2 中完成 SEO 友好 URL 所需的全部。 我一直在挣扎,然后我从@deacs 的回答中得到了帮助,我想这也许会对某人有所帮助。

到目前为止,对我来说最简单、最灵活的解决方案是 symlinks。您可以将项目放在任何地方,并且只创建符号链接到您的主机强制目录。例如,将项目放在 ~/domains/example.com/project 中并创建指向 public_html 目录的符号链接。

cd ~/domains/example.com
# remove old public_html directory
mv public_html old_public_html
# create symlink for fronted
ln -s ./project/frontend/web public_html
# create symlink for backend in /admin subdirectory
ln -s ./project/backend/web public_html/admin

还有 http://example.com/ 前端和 http://example.com/admin/ 后端。

如果您需要单独域中的后端 (admin.example.com):

ln -s ~/domains/example.com/project/backend/web ~/domains/admin.example.com/public_html

优点

  1. 没有几乎没人理解的扭曲重写规则。
  2. 您在 public 目录中没有整个项目,因此网络服务器 and/or PHP 的一些错误配置将不会 public 您的代码和配置文件。
  3. 您可以使项目适应托管所需的任何结构。
  4. 如果您使用相对符号链接,您可以将其置于版本控制之下,并创建一个将所有应用程序作为子目录的 webroot。

缺点

  1. 如果您的主机具有一些受限制的 open_basedir 设置而无法对其进行任何控制,您可能无法使用它。

将 .htaccess 放入 web 目录

代码

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