nshah143 nshah143 - 1 year ago 67
PHP Question

problems when hiding the 'web' folder from the url in YII2 basic application

The YII2 basic app is installed under localhost in 'ims' folder.The links are like (homepage) (about us page)

So far what i have done is.

1) in web/.htaccess file

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

2) in root .htaccess

Options -Indexes
RewriteEngine on
RewriteRule ^(.*)$ web/$1 [L]

3) In config/web.php

'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'baseUrl' => '/',

This fixes the following things:

1) Links are SEO friendly now

2) index.php does not show now in url

3) The homepage can be accessed with

The about , contact & login links now change to

It misses the base folder name in the url 'ims'. Any suggestions or ideas regarding this ?

Note:- I do not wish to use the Apache configuration to achieve this and also i do not wish to move the contents of web folder outside.
I wish to hide the 'web' from the url without changing the structure of the YII2 basic application.


I am answering my own question here :-

the only change that i needed to make it work to

'baseUrl' => '/', to 'baseUrl' => '/ims',

So the changed code looks like

    'components' => [
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName'  => false,
            'baseUrl' => '/ims',

Now i can browse all the pages without text 'web' in it. And this is achieved without making any apache configurations or moving the web folder in the root. :)