0

Change url frontend/web in Yii2

Demo here

The default ‘frontend’ url in yii2 is

http://yoursite.com/frontend/web/

Now, we will change it to 

http://yoursite.com

change url frontent web in yii2

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]

create htaccessOptions -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

Screenshot from 2015-07-29 12:24:143) open frontend/config/main.php, add the following script :

return [
	.....
	'homeUrl' => '/',
    'components' => [
		'request' => [
			'baseUrl' => '/',
		],
		.....
		'urlManager' => [
            'baseUrl' => '/',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => []
        ]
    ],
	....
];

config main yii24. Test your website! 🙂

reference : http://www.yiiframework.com/wiki/755/how-to-hide-frontend-web-in-url-addresses-on-apache/

Ambar Hasbiyatmoko

Hello, I'm web developer. Passionate about programming, web server, and networking.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.