Last active
March 11, 2021 11:12
-
-
Save erycamel/bb0e5653977d620e68ce to your computer and use it in GitHub Desktop.
Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Step 1 | |
Create .htaccess file in root folder, i.e advanced/.htaccess and write below code. | |
--------------- | |
Options +FollowSymlinks | |
RewriteEngine On | |
# deal with admin first | |
RewriteCond %{REQUEST_URI} ^/(admin) <------ | |
RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L] | |
RewriteRule ^admin/css/(.*)$ backend/web/css/$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] | |
RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/ <------ | |
RewriteCond %{REQUEST_URI} !index.php | |
RewriteCond %{REQUEST_FILENAME} !-f [OR] | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule ^.*$ frontend/web/index.php | |
--------------- | |
Note : if you are trying in local server then replace ^/ with ^/project_name/ where you see arrow sign. | |
Remove those arrow sign <------ after setup is done. | |
Step 2 | |
Now create a components/Request.php file in common directory and write below code in this file. | |
--------------- | |
<?php | |
namespace common\components; | |
class Request extends \yii\web\Request { | |
public $web; | |
public $adminUrl; | |
public function getBaseUrl(){ | |
return str_replace($this->web, "", parent::getBaseUrl()) . $this->adminUrl; | |
} | |
/* | |
If you don't have this function, the admin site will 404 if you leave off | |
the trailing slash. | |
E.g.: | |
Wouldn't work: | |
site.com/admin | |
Would work: | |
site.com/admin/ | |
Using this function, both will work. | |
*/ | |
public function resolvePathInfo(){ | |
if($this->getUrl() === $this->adminUrl){ | |
return ""; | |
}else{ | |
return parent::resolvePathInfo(); | |
} | |
} | |
} ?> | |
--------------- | |
Step 3 | |
Installing component. Write below code in frontend/config/main.php and backend/config/main.php files respectively. | |
------------------------- | |
//frontend, under components array | |
'request'=>[ | |
'class' => 'common\components\Request', | |
'web'=> '/frontend/web' | |
], | |
'urlManager' => [ | |
'enablePrettyUrl' => true, | |
'showScriptName' => false, | |
], | |
------------------------- | |
// backend, under components array | |
'request'=>[ | |
'class' => 'common\components\Request', | |
'web'=> '/backend/web', | |
'adminUrl' => '/admin' | |
], | |
'urlManager' => [ | |
'enablePrettyUrl' => true, | |
'showScriptName' => false, | |
], | |
------------------------- | |
Step 4 (Optional, if doesn't work till step three) | |
create .htaccess file in web directory | |
------------------------- | |
RewriteEngine On | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule ^(.*)$ /index.php?/$1 [L] | |
------------------------- | |
Note: make sure you have enabled your mod rewrite in apache | |
Thats it! You can try your project with | |
www.project.com/admin, www.project.com | |
in local server | |
localhost/project_name/admin, localhost/project_name | |
http://stackoverflow.com/questions/28118691/yii2-htaccess-how-to-hide-frontend-web-and-backend-web-completely |
great article!
thanks
Thanks. it works on localhost but not work on online host. what's the solution?
It works smoothly. found the step by step instructions.
Great Article...
Nice Step by step Instructions...
Perfect step by step... Great Article.... Thank you Ery Camel
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The best step by step instruction.
Thanks :)