Easy way to redirect http:\\localhost\yii2-basic to http:\\localhost\yii2-basic\web\index.php

First of all, install yii2 basic template according below link:

How to install yii2 basic template

Then, normally you have to use http:\localhost\yii2-basic\web to launch your homepage. And our target is both http:\localhost\yii2-basic and http:\localhost\yii2-basic\web... can launch your home page.


1. Create .htacess file both in your localhost\yii2-basic and localhost\yii2-basic\web folder the .htacss file look like below (which I copy from Kartik template:)

# ----------------------------------------------------------------------
# Adds some security for the Apache server configuration for use with
# ----------------------------------------------------------------------

# "-Indexes" will have Apache block users from browsing folders without a default document
# Usually you should leave this activated, because you shouldn't allow everybody to surf through
# every folder on your server (which includes rather private places like CMS system folders).
<IfModule mod_autoindex.c>
  Options -Indexes

# Block access to "hidden" directories whose names begin with a period. This
# includes directories used by version control systems such as Subversion or Git.
<IfModule mod_rewrite.c>
  RewriteCond %{SCRIPT_FILENAME} -d
  RewriteCond %{SCRIPT_FILENAME} -f
  RewriteRule "(^|/)\." - [F]

# Block access to backup and source files
# This files may be left by some text/html editors and
# pose a great security danger, when someone can access them
<FilesMatch "(\.(bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$">
  Order allow,deny
  Deny from all
  Satisfy All

# Increase cookie security
<IfModule php5_module>
  php_value session.cookie_httponly true

# Settings to hide index.php and ensure pretty urls
RewriteEngine on

# 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

2.Create another index.php in your localhosts\yii2-basic folder. The content only below:

header("location: /yii2-basic/web/");

That's all.

In addition, you can use beautiful url too:


Add below code

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,

into your config array which locate at localhost\yii2-basic\config\web.php