Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY
If your only goal is to achieve not ever seeing /frontend/web
or /backend/web
, even without using .htaccess rules, you could go for the following:
Why not just pull out the contents of the web
folders and place them in the root? Just adjust the path referring to the framework and config files in the entry scripts index.php
.
Your dir structure would look like:
- yii2app/
- frontend/
- backend/
- common/
- .. other folders..
- admin/
- assets/
- css/
- index.php
- assets/
- css/
- index.php
Your yii2app/index.php
would then look like:
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');
require(__DIR__ . '/common/config/bootstrap.php');
require(__DIR__ . '/frontend/config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/common/config/main.php'),
require(__DIR__ . '/common/config/main-local.php'),
require(__DIR__ . '/frontend/config/main.php'),
require(__DIR__ . '/frontend/config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();
and your yii2app/admin/index.php
would look like:
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');
require(__DIR__ . '/../common/config/bootstrap.php');
require(__DIR__ . '/../backend/config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../common/config/main.php'),
require(__DIR__ . '/../common/config/main-local.php'),
require(__DIR__ . '/../backend/config/main.php'),
require(__DIR__ . '/../backend/config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();
EDIT: your entry scripts could look different to mine, but you should get the idea of changing the paths to find the framework files with these examples.
How to remove frontend/web from url in Yii2 advanced so that it doesn't appear when I use Url::to
Use the following configuration in Apache's httpd.conf file or within a virtual host configuration. Note that you should replace path/to/frontend/web
with the actual path for frontend/web
.
# Set document root to be "frontend/web"
DocumentRoot "path/to/frontend/web"
<Directory "path/to/frontend/web">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# ...other settings...
</Directory>
Yii2 Docs
Yii2 - Hide backend/web from URL and use it like app/admin
I suggest you to move your backend\web\index.php file to root folder
then rename it to admin.php and change content to:
<?php
defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';
require __DIR__ . '/common/config/bootstrap.php';
require __DIR__ . '/backend/config/bootstrap.php';
$config = yii\helpers\ArrayHelper::merge(
require __DIR__ . '/common/config/main.php',
require __DIR__ . '/common/config/main-local.php',
require __DIR__ . '/backend/config/main.php',
require __DIR__ . '/backend/config/main-local.php'
);
(new yii\web\Application($config))->run();
and change .htaccess to:
RewriteOptions inherit
RewriteEngine On
# for backend and control panel
RewriteRule ^admin(.*) admin.php
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Options -Indexes
DirectoryIndex index.php
your directory must be like this :
- assets
- backend // remove web directory from backend
- common
- console
- frontend // and you can remove web directory from frontend too
- vendor
- index.php // index of frontend
- admin.php // index of backend
at the end;
www.yoursite.com/site/index => frontend/site/index
www.yoursite.com/admin/site/index => backend/site/index
how to remove url (/web/index.php) yii 2 and set route with parameter with clean url?
For advanced application follow these steps:
1) Add the following htaccess
to frontend/web
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
2) Add the following htaccess
to root folder
where application is installed
# prevent directory listings
Options -Indexes
IndexIgnore */*
# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1
3) Edit your frontend/config/main.php
file with the following at the top
use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());
4) Add the request component
to the components array in the same file i.e frontend/config/main.php
'components' => [
'request' => [
'baseUrl' => $baseUrl,
],
],
That's it.Now you can access the frontend without web/index.php
For you second question you need to write the rule for it in your URL manager component.
Something like this:
'urlManager' => [
'baseUrl' => $baseUrl,
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'transaction/getrequestdetail/<id>' => 'transaction/getrequestdetail',
),
],
Yii2 | Redirect to backend/frontend with htaccess
Although I had enabled mod_rewrite
through the terminal via the following command:
sudo a2enmod rewrite
But it was not enabled and I manually activated it from /etc/apache2/apache2.conf
.
Related Topics
How to Programmatically Login/Authenticate a User
How to Enable Curl in Wamp Server
Insert - Number of Bind Variables Doesn't Match Number of Fields in Prepared Statement
PHP - Redirect and Send Data Via Post
How to Make Dot Match Newline Characters Using Regular Expressions
JavaScript: How to Create Jsonp
PHP Fatal Error: Class 'Pdo' Not Found
Retrieving Multiple Result Sets With Stored Procedure in PHP/MySQLi
Convert Latin1 Characters on a Utf8 Table into Utf8
How to Access MySQL Result Set Data With a Foreach Loop
How to Re-Index the Values of an Array in PHP
Regular Expression Pattern to Match Url With or Without Http://Www
Session Data Lost in Chrome Only
Get Absolute Path of Initially Run Script