Home >Backend Development >PHP Tutorial >How to Hide Frontend and Backend Paths in Yii2 Applications?
Hiding Frontend and Backend Paths in Yii2 using .htaccess and Request Component
Problem Overview
Yii2 applications by default display frontend and backend paths in URLs. This can be undesirable for a clean and user-friendly experience. The goal is to make the entire site accessible without revealing these paths.
Solution
To achieve this, a combination of .htaccess configurations and a custom Request component is used.
Step 1: .htaccess Configuration
Create an .htaccess file in the root directory (i.e., advanced/.htaccess) with the following code:
Options +FollowSymlinks RewriteEngine On # Handle admin URL first 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 URL 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] 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
Step 2: Custom Request Component
Create a components/Request.php file in the common directory and add the following code:
<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>
Step 3: Component Configuration
In the frontend/config/main.php and backend/config/main.php files, add the following under the components array:
<code class="php">// frontend 'request' => [ 'class' => 'common\components\Request', 'web' => '/frontend/web' ], 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, ], // backend 'request' => [ 'class' => 'common\components\Request', 'web' => '/backend/web', 'adminUrl' => '/admin' ], 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, ],</code>
Optional Step 4: .htaccess File in Web Directory
If the URLs still contain the frontend/backend paths, create a .htaccess file in the web directory with the following lines:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?/ [L]
Conclusion
With these modifications, the Yii2 application will now operate without displaying the frontend and backend paths in the URLs, providing a more user-friendly and secure front-end experience.
The above is the detailed content of How to Hide Frontend and Backend Paths in Yii2 Applications?. For more information, please follow other related articles on the PHP Chinese website!