Yii 2.0 hiding / base / web connection with url along with index.php
Lots of information online on how to hide index.php from your Yii 2.0 application url, however, I am trying to do this to remove "/ basic / web /" from the url. / Basic / web is a directory, from which the application is running and the configuration I have is as follows. This goes into the config file:
'urlManager' =>[
'enablePrettyUrl' => true,
'showScriptName' => false,
],
And this is my htaccess file that I have in the / web folder:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
So far, so good, I can access something directly callable mysite.com/basic/web/controller/action
. What do I need to do to remove / basic / web so that the url is simple mysite.com/controller/action
?
Any advice is appreciated, thanks!
EDIT: I'm looking for a way without touching the apache config file as I don't have access to it.
source to share
You must define your apache config differently. Your site should be pointing to {folder} / basic / web, not {folder}.
Since you changed the requirements:
To configure cpanel you need to:
1) delete the stupid base folder, what's the point? Just because Yii installs it this way doesn't mean you should keep it. So, move all 1 level up.
2) Rename web to public_html, make sure you rename it in some files too (/ bootstrap config comes to mind).
Yes, you can do it with .htaccess, but you shouldn't have any files open on the internet, only your web folder should be open, so I don't give you this solution because it's not very good.
source to share
RewriteEngine on
# Change yourdomain.com to be your primary domain.
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$
RewriteCond %{REQUEST_URI} !^/basic/web/
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /basic/web/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$
RewriteRule ^(/)?$ basic/web/index.php [L]
Change .htaccess permissions to 440 when done. There is nothing to be afraid of using this method, contrary to what is indicated by Mihai P.
source to share
A working solution is here!
Step 1.in the base yii2 applications directory create index.php file and fill it like below
<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/config/web.php';
(new yii\web\Application($config))->run();
Step 2. Create a .htaccess file in the same base directory as below.
RewriteEngine on
# 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
source to share