Search code examples
php.htaccessurl-rewritingyii2yii-url-manager

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

http://192.168.0.99/ims/web/ (homepage)

http://192.168.0.99/ims/web/index.php?r=site%2Fabout (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 http://192.168.0.99/ims/

Issue:- The about , contact & login links now change to

http://192.168.0.99/site/about

http://192.168.0.99/site/contact

http://192.168.0.99/site/login

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.


Solution

  • 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. :)