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

  1. Move frontend/web and backend/web to the root of your project:

    You can avoid directly accessing the web directories by pointing Apache to the frontend/web and backend/web directories as the root directory.

    • Open the .htaccess file in the frontend/web directory and move it to the frontend/ 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 .htaccess file in the backend/web directory and move it to the backend/ 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]
  2. Update Apache VirtualHost Configuration:

    In your Apache config file (for example, 000-default.conf), update the DocumentRoot to point to your frontend/web directory for the frontend and to /backend/web for /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:

  1. 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;
    }
    }


  2. 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 to true to enable pretty URLs (without index.php).
  • showScriptName: Set this to false to remove index.php from the URL.
  • The rules array 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

  1. Permissions: Make sure that the frontend/web and backend/web directories have the correct permissions so that your web server can serve them properly.
  2. 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.
  3. Config for Console: If you are using console commands in Yii2 (for example, yii migrate), make sure the console/config/main.php is 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.

Share!

Shares