【问题标题】:Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELYYii2 htaccess - 如何完全隐藏前端/网络和后端/网络
【发布时间】:2015-03-23 00:15:32
【问题描述】:

我想我已经很接近了。我将 htaccess 重定向到网站(前端/网络)和/admin 路径(backend/web)。网站看起来不错,CSS 文件加载等。

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

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

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

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

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

.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 :)

我尝试在根目录的 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 高级设置,使用作曲家,并遵循文档。到目前为止,我唯一玩过的是提到的 htaccess 文件。

谢谢!

【问题讨论】:

  • 或者...您可以学习如何在您的计算机上设置虚拟主机并完成类似这样的骇人听闻的事情。你为什么要制造一个问题,然后试图找到一个愚蠢的解决方案? Google 虚拟主机和您拥有的任何操作系统,并学习以正确的方式进行操作
  • 我已经知道 vhosts,虽然我觉得我不应该为 Yii2 测试箱创建一个虚拟主机来解决这个问题。
  • 您不必对此无礼。如果您太费心回答或提供帮助,请不要发帖并继续前进。为什么我要为我运行或测试的每一件事设置 100 个虚拟主机...
  • 您可以为您的域定义您的虚拟主机的 DocumentRoot 与 web 目录(/path to directory/backend/web),因此它通过进入域而不显示它来打开 web 目录。您不一定需要添加 htaccess 规则。

标签: php apache .htaccess mod-rewrite yii2


【解决方案1】:

用 .htaccess 方法试试这个 -

第 1 步

在根目录下创建 .htaccess 文件,即advanced/.htaccess 并写入以下代码。

Options +FollowSymlinks
RewriteEngine On

# deal with admin first
RewriteCond %{REQUEST_URI} ^/(admin) <------
RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [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/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteRule ^js/(.*)$ frontend/web/js/$1 [L] 
RewriteRule ^images/(.*)$ frontend/web/images/$1 [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/。设置完成后删除那些箭头符号&lt;------

第 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?/$1 [L]

注意:确保您已在 apache 中启用您的 mod 重写

就是这样!您可以通过
www.project.com/admin, www.project.com

试用您的项目

在本地服务器中
localhost/project_name/admin, localhost/project_name

【讨论】:

  • 我使用了这个答案,但在 Windows 服务器中安装系统时遇到问题。如何将 .htaccess 转换为 web.config
  • 这对我有用,但我现在有另一个项目,我没有前端,只有一个后端,我怎样才能重写这个,所以 '/' 去后端网络? (而且我不能只使用基本模板)
  • 我正在开发 yii2 的高级版本,我想将这个 /frontend/home/jobs 重写为 /jobs/。我试过你的答案,但在此之后我得到异常错误和无效的文件目录。那我该怎么做呢
  • 在前端 web 目录中创建一个 htaccess 文件并将此代码写入其中 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?/$1 [L] 确保您已启用 mod-rewrite。
  • 非常感谢,但是您忘记了第一个 .htaccess 中的 javascript 和图像: RewriteRule ^js/(.*)$ frontend/web/js/$1 [L] RewriteRule ^images/(. *)$ frontend/web/images/$1 [L]
【解决方案2】:

如果您的唯一目标是实现从未见过 /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();

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

【讨论】:

  • 移动目录是一个想法。我一直在寻找 htaccess 方法来保持运行 composer 后的所有内容。我不知道将来移动这些目录会对运行作曲家产生什么不利影响。我认为 htaccess 可以让我准确地保持结构开箱即用的方式。
  • 您可以在更新时很好地使用 composer,并且该应用程序也将按您的预期运行。所以基本上没有缺点,所以这种方法恕我直言。
  • 谢谢兄弟。这对我帮助很大。正是我想要的。只需在 admin 和 root 目录中添加基本模板的 htaccess 代码,它就像一个魅力
  • @beginner 那么你做错了什么。无论版本如何,此方法始终适用于 yii2
  • 是的,我做错了什么。即使在基本模板中,上述内容也适用。
【解决方案3】:

就像@deacs 所说,只需将文件从前端/web 移动到 yii2app(根文件夹),然后在 yii2app“admin”中创建一个文件夹,然后将文件从后端/web 移动到 yii2app/admin,然后在两个 admin 中创建 .htaccess 文件和 yii2app 的代码如下:

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

然后在frontend/config/main.php和backend/config/main.php的配置文件main.php中添加/修改urlManager组件,代码如下:

    '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 的回答中得到了帮助,我虽然也许这会对某人有所帮助。

【讨论】:

    【解决方案4】:

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

    更改您帐户的主域名,然后将旧的主域添加为插件域。这样,您可以为主域/新插件域选择所需的根文件夹。 (新的主域现在将指向 public_html。)

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

    【讨论】:

      【解决方案5】:

      我关注了“deacs”的回答并得到了以下错误

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

      然后我在“yii2app”中创建了“assets”文件夹,它可以工作了

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

      无需移动文件,您可以点击以下链接

      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

      【讨论】:

        【解决方案6】:

        到目前为止,对我来说最简单和最灵活的解决方案是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. 您的公共目录中没有整个项目,因此网络服务器和/或 PHP 的一些错误配置不会公开您的代码和配置文件。
        3. 您可以使项目适应托管所需的任何结构。
        4. 如果您使用相对符号链接,您可以将其置于版本控制之下,并创建单个 webroot 并将所有应用程序作为子目录。

        缺点

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

        【讨论】:

          【解决方案7】:

          将 .htaccess 放入网络目录

          代码

          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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-11-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多