Yii2 how to remove web from URL

  • Last update: Apr 3, 2024
  • Views: 51
  • Author: Admin
Yii2 how to remove web from URL

Colleagues hello to all.

In today's small article, I'll show you how to remove from a web address in yii2. Immediately after installing your site, it is available at yii2/web, because the entire public part of your site is located in the web directory. If we simply navigate to the site root then we will see a list of the root folder. Let's make the main page of the site available when accessing the domain name, without adding the  web directory.

 

yii2_delete_web

Well, this option is definitely not suitable for us.


 

The first thing to do is go to the site's configuration file called web.php and fix something in it.

1. Find a parameter in this file called  urlManager, it will be commented out by default. On the contrary, we need to uncomment it.

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
    ],
],

yii2_delete_web

 

2. Find in the same file a parameter called request, and we need to add to it:

 'baseUrl'=> '',

yii2_delete_web

That's it, we're done with the configuration file.


 

The next steps we need to create are two .htaccess files. This file is responsible for managing the apache web server itself or nginx.

We need to create the first  .htaccess file of the web directory itself, and add to it:

RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

yii2_delete_web

 

We need to create the second .htaccess file in the root of the site, and add to it:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1  [L]
RewriteRule ^js/(.*)$ web/js/$1  [L]
RewriteRule ^images/(.*)$ web/images/$1  [L]
RewriteRule (.*) /web/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php

yii2_delete_web


 

That's it, now when you visit the site, the web will no longer be in the address bar.

yii2_delete_web


 

Thank you all, I hope that my article helped you in some way.

SIMILAR ARTICLES