How to change base url : http://Domain/ProjectName/web/index.php to http://Domain/ProjectName in Yii2.
Asked
Active
Viewed 146 times
2 Answers
0
You should enable pretty url in app/config/main.php (in advanced template) of /web.php in basic template
$config = [
//...
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
],
//...
for apache add this to your ProjectName/web/.htaccess file
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteBase /ProjectName/
# 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?r=$1 [L,QSA]
see this for more Enable clean URL in Yii2
0
Let say you have following directory structure:
/root
|
|--/logs // or any other folder
|--/html <-- this is where DOMAIN.com is pointing
|
|--/ProjectName
|
|--/assets
|--/web
|--// and other folder
what you can do is move your project folder outside of the html
. then copy only the web
folder of your app inside your html
folder and rename it to ProjectName
(or any other). now your folder structure should look something like this:
/root
|
|--/logs // or any other folder
|--/html <-- this is where DOMAIN.com is pointing
|
|--/ProjectName
|
|--/assets
|--/css
|--/index.php <-- EDIT this one
|--// and other files you had in web folder
|--/ProjectName
|
|--/assets
|--/web
|--// and other folder
finally open the index.php
(file pointed above) and replace following lines:
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
with this:
$project_path = __DIR__ . '/../../PojectName';
require($project_path . '/vendor/autoload.php');
require($project_path . '/vendor/yiisoft/yii2/Yii.php');
$config = require($project_path . '/config/web.php');
that's it. now you can access your application at: http://DOMAIN/ProjectName
hope that helps. if not let me know in the comment.

leninhasda
- 1,620
- 11
- 18