skip to Main Content

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' => 'yiiwebUrlManager',
            '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.

3

Answers


  1. Chosen as BEST ANSWER

    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' => 'yiiwebUrlManager',
                '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. :)


    1. In web/.htaccess file
    Options +FollowSymLinks
    IndexIgnore */*
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php
    
    1. in root .htaccess
    Options -Indexes
    RewriteEngine on
    RewriteRule ^(.*)$ web/$1 [L]
    
    1. In config/web.php
    use yiiwebRequest;
    $baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());
    
    'components' => [
            'request' => [
                'baseUrl' => $baseUrl,
            ],
            'urlManager' => [
                'class' => 'yiiwebUrlManager',
                'enablePrettyUrl' => true,
                'showScriptName'  => false,
                'baseUrl' => '/',
            ],
    
    Login or Signup to reply.
  2. So far this worked for me finally

    1. In web/.htaccess file

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

    2. Then in root .htaccess

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

    3. In config/web.php

    <code>
        use yiiwebRequest;
        $baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());
        'components' => [
            'request' => ['baseUrl' => $baseUrl,],
            'urlManager' => [
                'class' => 'yiiwebUrlManager',
                'enablePrettyUrl' => true,
                'showScriptName'  => false,
            ],
    </code>
    

    NB: Just remove the ‘baseUrl’=>’/’ in the config/web.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search