How to remove "web" in Yii URl without creating virtual host

How to remove "web" in Yii URl without creating virtual host.

Client have shared server & i can not create virtual host.

Wordpress is my root site & yii2 is my subfolder. I just want to get rid of "web" from url.

Thanks,

Hemant

try this (advanced template):

add htaccess to your root:


<IfModule mod_rewrite.c>

    Options +FollowSymlinks

    RewriteEngine On

</IfModule>


<IfModule mod_rewrite.c>

    # deal with admin first

    RewriteCond %{REQUEST_URI} ^/(admin)

    RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L]

    RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [L]

    RewriteRule ^admin/js/(.*)$ backend/web/js/$1 [L]


    RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/

    RewriteCond %{REQUEST_URI} ^/(admin)

    RewriteRule ^.*$ backend/web/index.php [L]


    RewriteCond %{REQUEST_URI} ^/(assets|css)

    RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]

    RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]

    RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]


    RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/

    RewriteCond %{REQUEST_URI} !index.php

	RewriteCond %{REQUEST_URI} !^/preview

    RewriteCond %{REQUEST_FILENAME} !-f [OR]

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^.*$ frontend/web/index.php

</IfModule>

add htaccess to backend/web:


# if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d


# otherwise forward it to index.php

RewriteRule . index.php


Options FollowSymLinks

Add Request.php to common/components:


<?php

namespace common\components;


/**

 * This extension of the Request component can be used to replace parts of the 

 * requested url.

 * 

 * It has to be enabled in the 'components' area of the main configuration files

 * for the front- and backend:

 * 

 * eg: If you want to replace '/frontend/web' from the url's, put this in

 *     frontend/config.main.php in the 'components' section.

 * 

 *      'request'=>[

 *          'class' => 'common\components\Request',

 *          'web'=> '/frontend/web'

 *      ]

 * 

 *

 */

class Request extends \yii\web\Request

{

    public $web;

    public $adminUrl;


    /**

     * Takes the base url from the parent class and replaces the 'web' url that

     * you defined with an empty string:

     * 

     *  eg: the 'web' url is set to 'frontend/web'

     *      www.mydomain.com/frontend/web becomes www.mydomain.com/

     * 

     * @return  string

     */

    public function getBaseUrl()

    {

        return str_replace($this->web, '', parent::getBaseUrl()) . $this->adminUrl;

    }




    /**

     * This function ensures that www.mydomain.com/admin (without trailing slash) will not

     * throw a 404 error

     * 

     * @return  string

     */

    public function resolvePathInfo()

    {

        if ($this->getUrl() === $this->adminUrl) {

            return '';

        } else {

            return parent::resolvePathInfo();

        }

    }

}

Add to components in backend/config/main.php:


'request'=>[

            'class' => 'common\components\Request',

            'web'=> '/backend/web',

            'adminUrl' => '/admin'

        ],

So your backend will be /admin

Add to components in backend/config/main.php:


        // Rewrite url's

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName'  => false,

        ],

I’m not sure if this is everything…

Hi,

An other solution is to use directly a template recommanded for shared host

  • Improved Basic template

  • Improved Advanced template

I use the advanced one for my site… just fine

Jok

Hi Jok,

Many Thanks for your solution.

Improved Basic template

&

Improved Advanced template

are good alternative.

@Hashie : Thanks for your solution too. I have not get time to go with them , but i will try it when get time.

Thanks,

Hemant

My pleasure ;)