U
U
urfinjazz2015-04-17 12:18:49
Yii
urfinjazz, 2015-04-17 12:18:49

How to remove web/ from url in Yii2 basic using .htaccess and urlManager rules?

Hello!
I must say right away that I was looking for a solution to the problem in Google, there are many solutions, but none of them came up for some reason.
While learning Yii2, I encountered such a problem:
you need to get the url domen/controller/action,
but any request like domen/anything redirects to defaultRoute
requests like domen/web/controller/action work fine
in the web.php config now:

'urlManager' => [
      'showScriptName' => false,
      'enablePrettyUrl' => true,
      'rules' => [
        '<controller>/<action>' => '<controller>/<action>'
      ]
    ],

in the root of the site in .htaccess:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule (.*) /web/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php

in the web folder in .htaccess:
RewriteEngine On RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

I would be very grateful for help in solving the problem!

Answer the question

In order to leave comments, you need to log in

8 answer(s)
U
urfinjazz, 2015-04-17
@urfinjazz

I will answer my own question :)
thanks, Ivan , for the tip
.htaccess in the web, we leave the same
.htaccess in the root of the site:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
RewriteRule ^js/(.*)$ web/js/$1 [L]
RewriteRule ^images/(.*)$ web/images/$1 [L]
RewriteRule (.*) /web/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php

in the web.php config in 'request' add the line:
Thank you all for your attention!

I
Ivan, 2015-04-17
@IvanCher

In short, you need to specify WebRoot not the root folder with the project (***/project), but the web folder (***/project/web).

P
phpnt, 2015-04-19
@phpnt

How to remove web in this video , how to use UrlManager in this

R
Rikcon, 2015-04-17
@Rikcon

Do you have php attached to Apache?
via mod_php or via php-fpm.
If via php-fpm then throw off the virtualhost config here, you need to correct one line there.

Y
Yan_Alex, 2017-05-10
@Yan_Alex

It worked for me after adding 'baseUrl'=> '', htaccess remained original, the same as the author in the first post

N
Nikolai, 2018-10-30
@grachyoff

Solving the problem for Openserver. In the root of the site, next to the basic folder, create htaccess:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ basic/web/assets/$1 [L]
RewriteRule ^css/(.*)$ basic/web/css/$1 [L]
RewriteRule ^js/(.*)$ basic/web/js/$1 [L]
RewriteRule ^images/(.*)$ basic/web/images/$1 [L]
RewriteRule (.*) basic/web/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . basic/web/index.php

In web folder :
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

Also add the line baseUrl =>'' to the config in request and uncomment the block:
'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],

T
Tsarev Vadim, 2019-03-02
@zava75

An error has arrived related to the cache on the open server php7 / can anyone know how to win?

PHP Warning – yii\base\ErrorException
filemtime(): stat failed for D:\OpenServer\domains\yii2basic.ru\runtime/cache\60\60c69796c9cdfd66af3195782c1d16d7.bin
1. in D:\OpenServer\domains\yii2basic.ru\vendor\yiisoft\yii2\caching\FileCache.php at line 113
104105106107108109110111112113114115116117118119120121122                         * Retrieves a value from cache with a specified key.
     * This is the implementation of the method declared in the parent class.
     * @param string $key a unique key identifying the cached value
     * @return string|false the value stored in cache, false if the value is not in the cache or expired.
     */
    protected function getValue($key)
    {
        $cacheFile = $this->getCacheFile($key);
 
        if (@filemtime($cacheFile) > time()) {
            $fp = @fopen($cacheFile, 'r');
            if ($fp !== false) {
                @flock($fp, LOCK_SH);
                $cacheValue = @stream_get_contents($fp);
                @flock($fp, LOCK_UN);
                @fclose($fp);
                return $cacheValue;
            }
        }
                
2. in D:\OpenServer\domains\yii2basic.ru\vendor\yiisoft\yii2\caching\Cache.php at line 134 – yii\caching\FileCache::getValue('key' => ''60c69796c9cdfd66af3195782c1d16d...')
3. in D:\OpenServer\domains\yii2basic.ru\vendor\yiisoft\yii2\web\UrlManager.php at line 296 – yii\caching\Cache::get('key' => ''60c69796c9cdfd66af3195782c1d16d...')
4. in D:\OpenServer\domains\yii2basic.ru\vendor\yiisoft\yii2\web\UrlManager.php at line 229 – yii\web\UrlManager::getBuiltRulesFromCache('ruleDeclarations' => 'array (0 => array ('class' => 'y...')
5. in D:\OpenServer\domains\yii2basic.ru\vendor\yiisoft\yii2\web\UrlManager.php at line 211 – yii\web\UrlManager::buildRules('ruleDeclarations' => 'array (0 => array ('class' => 'y...')
6. in D:\OpenServer\domains\yii2basic.ru\vendor\yiisoft\yii2-debug\src\Module.php at line 215 – yii\web\UrlManager::addRules('rules' => 'array (0 => array ('class' => 'y...', 'append' => 'FALSE')
209210211212213214215216217218219220221                                [
                'class' => 'yii\web\UrlRule',
                'route' => $this->id . '/<controller>/<action>',
                'pattern' => $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>',
                'suffix' => false
            ]
        ], false);
    }
 
    /**
     * {@inheritdoc}
     */
    public function beforeAction($action)
                
7. in D:\OpenServer\domains\yii2basic.ru\vendor\yiisoft\yii2\base\Application.php at line 327 – yii\debug\Module::bootstrap('app' => 'class yii\web\Application { publ...')
8. in D:\OpenServer\domains\yii2basic.ru\vendor\yiisoft\yii2\web\Application.php at line 69 – yii\base\Application::bootstrap()
9. in D:\OpenServer\domains\yii2basic.ru\vendor\yiisoft\yii2\base\Application.php at line 273 – yii\web\Application::bootstrap()
10. in D:\OpenServer\domains\yii2basic.ru\vendor\yiisoft\yii2\base\BaseObject.php at line 109 – yii\base\Application::init()
11. in D:\OpenServer\domains\yii2basic.ru\vendor\yiisoft\yii2\base\Application.php at line 206 – yii\base\BaseObject::__construct('config' => 'array ('id' => 'basic', 'bootstr...')
12. in D:\OpenServer\domains\yii2basic.ru\web\index.php at line 12 – yii\base\Application::__construct('config' => 'array ('id' => 'basic', 'bootstr...')
6789101112                     
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
 
$config = require __DIR__ . '/../config/web.php';
 
(new yii\web\Application($config))->run();
                
$_COOKIE = [
    'PHPSESSID' => 'fvv6fua8cv84cvtbbddoab66h6',
    '_csrf' => '555228cc1e7ab80e85e0f45275012b60422d60f12addf3c56f67d1c97045e563a:2:{i:0;s:5:"_csrf";i:1;s:32:"pArGk2cTLQqNChEzFPQojeJtOtGjH1TH";}',
];

$_SESSION = [
    '__flash' => [],
];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question