nshah143
nshah143

Reputation: 559

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.

Upvotes: 1

Views: 2004

Answers (3)

user3056177
user3056177

Reputation: 39

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 \yii\web\Request;
    $baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());
    'components' => [
        'request' => ['baseUrl' => $baseUrl,],
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName'  => false,
        ],
</code>

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

Upvotes: 3

user3148289
user3148289

Reputation: 1

  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 \yii\web\Request;
$baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());

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

Upvotes: -1

nshah143
nshah143

Reputation: 559

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

Upvotes: 1

Related Questions