The default ‘frontend’ url in yii2 is
http://yoursite.com/frontend/web/
Now, we will change it to
http://yoursite.com
Let’s do it!
1) Create .htaccess on your root directory of yii, copy script below (ctrl + h if .htaccess is hidden) :
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
</IfModule>
# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>
# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]
Options -Indexes
used for preventing show of list file in your folder
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
used for routed to frontend/web. So if you access http://yourdomain.com, actually you access frontend/web.
<Files ~ “(.json|.lock|.git)”>
Order allow,deny
Deny from all
</Files>
used for deny file type : .json / .lock / .git on your root directory
2) Create .htaccess in /frontend/web, copy the following script :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
3) open frontend/config/main.php, add the following script :
return [
.....
'homeUrl' => '/',
'components' => [
'request' => [
'baseUrl' => '/',
],
.....
'urlManager' => [
'baseUrl' => '/',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => []
]
],
....
];
4. Test your website! 🙂
reference : http://www.yiiframework.com/wiki/755/how-to-hide-frontend-web-in-url-addresses-on-apache/
