To hide frontend/web and backend/web completely and enable clean URLs in Yii2, you need to configure both the Apache/Nginx server and Yii2 URL management settings. This involves setting up URL rewriting and enabling pretty URLs in Yii2.
Step 1: Modify Apache .htaccess or Nginx Configuration
To remove /frontend/web and /backend/web from the URL, you can either configure .htaccess for Apache or modify the server block in Nginx.
For Apache
- Move
frontend/webandbackend/webto the root of your project:You can avoid directly accessing the
webdirectories by pointing Apache to thefrontend/webandbackend/webdirectories as the root directory.- Open the
.htaccessfile in thefrontend/webdirectory and move it to thefrontend/directory. Then modify it as follows:
RewriteEngine On
# Redirect frontend requests to frontend/web
RewriteCond %{REQUEST_URI} !^/frontend/web/
RewriteRule ^(.*)$ frontend/web/$1 [L]
- Open the
.htaccessfile in thebackend/webdirectory and move it to thebackend/directory. Then modify it as follows:
RewriteEngine On
# Redirect backend requests to backend/web
RewriteCond %{REQUEST_URI} ^/admin
RewriteCond %{REQUEST_URI} !^/backend/web/
RewriteRule ^admin(.*)$ backend/web/$1 [L]
- Open the
- Update Apache VirtualHost Configuration:
In your Apache config file (for example,
000-default.conf), update theDocumentRootto point to yourfrontend/webdirectory for the frontend and to/backend/webfor/admin.<VirtualHost *:80>
ServerName localhost
DocumentRoot “/path/to/your/yii2app/frontend/web”
<Directory “/path/to/your/yii2app/frontend/web”>
AllowOverride All
Require all granted
</Directory>
</VirtualHost><VirtualHost *:80>
ServerName localhost
DocumentRoot “/path/to/your/yii2app/backend/web”
Alias /admin “/path/to/your/yii2app/backend/web”
<Directory “/path/to/your/yii2app/backend/web”>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
For Nginx
If you’re using Nginx, edit your configuration file like this:
- Create a server block for the frontend:
server {
listen 80;
server_name localhost;
root /path/to/your/yii2app/frontend/web;location / {
try_files $uri $uri/ /index.php?$args;
}location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}location ~ /\.ht {
deny all;
}
}
- Create a server block for the backend (admin):
server {
listen 80;
server_name localhost;
root /path/to/your/yii2app/backend/web;
location /admin {
try_files $uri $uri/ /admin/index.php?$args;
}location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}location ~ /\.ht {
deny all;
}
}
Step 2: Enable Pretty URLs in Yii2 (Clean URLs)
To remove index.php and ? from URLs, you need to configure Yii2’s URL Manager. This is done in the frontend/config/main.php and backend/config/main.php configuration files.
1. Update frontend/config/main.php:
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false, // This hides index.php from the URL
'enableStrictParsing' => false,
'rules' => [
'' => 'site/index',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
],
2. Update backend/config/main.php:
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false, // This hides index.php from the URL
'enableStrictParsing' => false,
'rules' => [
'' => 'site/index',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
],
enablePrettyUrl: Set this totrueto enable pretty URLs (withoutindex.php).showScriptName: Set this tofalseto removeindex.phpfrom the URL.- The
rulesarray defines the URL patterns and how they are mapped to controllers and actions.
Step 3: Test the Configuration
After updating your .htaccess/Nginx configuration and Yii2 configuration files, restart your server (Apache/Nginx).
Test URLs:
- For frontend:
http://localhost/yii2app/ - For backend (admin):
http://localhost/yii2app/admin
Both should now work without /frontend/web and /backend/web in the URL.
Additional Considerations
- Permissions: Make sure that the
frontend/webandbackend/webdirectories have the correct permissions so that your web server can serve them properly. - Cache and Sessions: If you are using caching or session handling in Yii2, make sure the paths are correctly set after the directory changes to avoid permission issues or broken sessions.
- Config for Console: If you are using console commands in Yii2 (for example,
yii migrate), make sure theconsole/config/main.phpis correctly set to work with the new folder structure.
Following these steps will remove frontend/web and backend/web from your URLs, enable clean URLs, and make your Yii2 application cleaner and more professional-looking.

Leave a Reply