Yii2 htaccess - 如何完全隐藏前端/网络和后端/网络 [英] Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY

查看:30
本文介绍了Yii2 htaccess - 如何完全隐藏前端/网络和后端/网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I think I am pretty close. I have the htaccess redirecting to the website (frontend/web) and the /admin path (backend/web). The site appears fine, CSS files loading, etc.

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

If you go to: http://localhost/yii2app/ - it loads the homepage, and doesn't redirect in the address bar, but the page shows frontend/web in all the URLs.

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

if you go to: http://localhost/yii2app/admin - it loads the backend login page, however it immediately redirects to /backend/web/site/login in the address bar (ugly).

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

Problem: The frontend/backend paths are showing in the URLs (address bar, and links on the page).

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

What I need: I want the whole site to operate without showing frontend/backend links. The project's root should pull (invisibly) from the frontend/web without showing it.. So http://localhost/yii2app/ runs my whole frontend, and http://localhost/yii2app/admin/ runs my whole backend.

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

Why? I feel this setup would be pretty solid and elegant when live on a server. I want to be able to push my project folder live to a site and it work just fine without having to have hacks to handle local vs server.

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

现在在前端和后端 Web 目录中,它们都具有相同的 .htaccess:

Now in frontend and backend web directories, they both have the same .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 :)

I do not want to see /frontend/web or /backend/web ever :)

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

I tried to play with the RewriteRule in the root's htaccess to add /admin to the URL, but it kept telling me /admin does not exist. I KNOW it does not exist, and I don't want it to exist. I want it to be a relative path.. ie: /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

Worded another way. I way everything in the project's root (http://localhost/yii2app/) to load frontend/web, but without showing it. Also, http://localhost/yii2app/admin to load backend/web and just showing http://localhost/yii2app/admin. Obviously they would have their respective controller/action attached to them. So admin could look like http://localhost/yii2app/admin/site/login

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

NOTE: I have not played with any of the files. This is a stock yii2 advanced setup, using composer, and following the docs. The only thing I have played with so far are the htaccess files mentioned.

谢谢!

推荐答案

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

If your only goal is to achieve not ever seeing /frontend/web or /backend/web, even without using .htaccess rules, you could go for the following:

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

Why not just pull out the contents of the web folders and place them in the root? Just adjust the path referring to the framework and config files in the entry scripts 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();

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

your entry scripts could look different to mine, but you should get the idea of changing the paths to find the framework files with these examples.

这篇关于Yii2 htaccess - 如何完全隐藏前端/网络和后端/网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆