簡體   English   中英

Yii2 htaccess - 如何完全隱藏前端/網絡和后端/網絡

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

我覺得我很接近。 我將 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/運行我的整個后端。

為什么? 我覺得這個設置在服務器上運行時會非常可靠和優雅。 我希望能夠將我的項目文件夾實時推送到一個站點,並且它可以正常工作而無需進行 hack 來處理本地 vs 服務器。

/yii2app目錄中的.htaccess文件:

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

我試圖在 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 高級設置,使用作曲家,並遵循文檔。 到目前為止,我唯一玩過的就是提到的 htaccess 文件。

謝謝!

用 .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/看到箭頭符號的位置。 設置完成后刪除那些箭頭符號<------

第2步

現在在 common 目錄中創建一個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 步(可選,如果在第三步之前不起作用)

在 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/adminlocalhost/project_name

如果您的唯一目標是實現永遠看不到/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();

編輯:您的入口腳本可能看起來與我的不同,但您應該了解更改路徑以使用這些示例查找框架文件。

就像@deacs 所說的,只需將文件從前端/web 移動到 yii2app(根文件夾)並在 yii2app“admin”中創建一個文件夾,然后將文件從后端/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

然后在 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 的答案中得到了幫助,我雖然這可能會對某人有所幫助。

在尋找最佳解決方案 2 天后,我采用了這種方式(托管在共享主機上)。 我創建子域admin.xxxxx.com並將其指向文檔根目錄/public_html/xxxxxx/backend/web
由於共享主機不允許您為主域放置自定義文檔根目錄,因此我使用以下解決方案:

更改您帳戶上的主域名,然后將舊的主域添加為附加域。 這樣,您可以為主域/新附加域選擇所需的根文件夾。 (新的主域現在將指向 public_html。)

然后指向(現在是我的插件域)到前端/public_html/xxxxxx/frontend/web右文檔根目錄

我按照“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

到目前為止,對我來說最簡單、最靈活的解決方案是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設置而無法對其進行任何控制,則您可能無法使用它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM