Yii2 htaccess-如何完全隐藏前端/网络和后端/网络

我想我很近。我将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 /运行我的整个后端。


为什么?我觉得当安装在服务器上时,此设置将非常牢固且优雅。我希望能够将我的项目文件夹实时推送到站点,并且可以正常工作而不必使用黑客来处理本地vs服务器。


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


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 / adminbackend/web并仅显示http:// localhost / yii2app / admin。显然,他们将拥有各自的controller/action依附。因此管理员看起来像http:// localhost / yii2app / admin / site / login


注意:我尚未播放任何文件。这是一个基于yii2的高级设置,使用composer并遵循了文档。到目前为止,我唯一玩过的就是提到的htaccess文件。


谢谢!


qq_笑_17
浏览 715回答 3
3回答

三国纷争

如果您的唯一目标是即使根本不使用.htaccess规则也不会看到/frontend/web或/backend/web,则可以执行以下操作:为什么不只是拉出web文件夹的内容并将其放在根目录中呢?只需在输入脚本中参考框架和配置文件来调整路径即可index.php。您的目录结构如下所示:- yii2app/&nbsp; &nbsp; - frontend/&nbsp; &nbsp; - backend/&nbsp; &nbsp; - common/&nbsp; &nbsp; - .. other folders..&nbsp; &nbsp; - admin/&nbsp; &nbsp; &nbsp; &nbsp; - assets/&nbsp; &nbsp; &nbsp; &nbsp; - css/&nbsp; &nbsp; &nbsp; &nbsp; - index.php&nbsp; &nbsp; - assets/&nbsp; &nbsp; - css/&nbsp; &nbsp; - 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(&nbsp; &nbsp; require(__DIR__ . '/common/config/main.php'),&nbsp; &nbsp; require(__DIR__ . '/common/config/main-local.php'),&nbsp; &nbsp; require(__DIR__ . '/frontend/config/main.php'),&nbsp; &nbsp; 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(&nbsp; &nbsp; require(__DIR__ . '/../common/config/main.php'),&nbsp; &nbsp; require(__DIR__ . '/../common/config/main-local.php'),&nbsp; &nbsp; require(__DIR__ . '/../backend/config/main.php'),&nbsp; &nbsp; require(__DIR__ . '/../backend/config/main-local.php'));$application = new yii\web\Application($config);$application->run();编辑:您的输入脚本可能看起来与我的不同,但是您应该想到更改路径以使用这些示例查找框架文件的想法。
打开App,查看更多内容
随时随地看视频慕课网APP