使用.htaccess 在Yii2 網站上隱藏前端/web 和後端/web 目錄
問題:
問題:在Yii2 Advanced 範本中,前端和後端目錄可以在網站的URL 中看到。對於更專業或客製化的外觀來說,這可能是不受歡迎的。
解決方案:Options +FollowSymlinks RewriteEngine On # Handle admin URL separately 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] # Handle frontend assets 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] # Handle frontend 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
要隱藏這些目錄,請修改網站根目錄中的.htaccess 文件,如下所示:
其他設定:
<code class="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; } public function resolvePathInfo() { if ($this->getUrl() === $this->adminUrl) { return ""; } else { return parent::resolvePathInfo(); } } }</code>
<code class="php">// frontend 'request' => [ 'class' => 'common\components\Request', 'web' => '/frontend/web' ], // backend 'request' => [ 'class' => 'common\components\Request', 'web' => '/backend/web', 'adminUrl' => '/admin' ],</code>
分別在frontend /config/main.php 和backend/config/main.php 中設定Request 元件:
Web 目錄的附加.htaccess 設定:RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?/ [L]
以上是如何在 Yii2 網站的 URL 中隱藏前端和後端目錄?的詳細內容。更多資訊請關注PHP中文網其他相關文章!