From [[https://gist.github.com/russianlagman/5c3d58a74c1753c60ef4]]

In root directory
<code>
Options FollowSymLinks
AddDefaultCharset utf-8

<IfModule mod_rewrite.c>
    RewriteEngine On

    # the main rewrite rule for the frontend application
    RewriteCond %{REQUEST_URI} !^/(backend/web|admin)
    RewriteRule !^frontend/web /frontend/web%{REQUEST_URI} [L]

    # redirect to the page without a trailing slash (uncomment if necessary)
    #RewriteCond %{REQUEST_URI} ^/admin/$
    #RewriteRule ^(admin)/ /$1 [L,R=301]
    
    # the main rewrite rule for the backend application
    RewriteCond %{REQUEST_URI} ^/admin
    RewriteRule ^admin(.*) /backend/web/$1 [L]

    # if a directory or a file of the frontend application exists, use the request directly
    RewriteCond %{REQUEST_URI} ^/frontend/web
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # otherwise forward the request to index.php
    RewriteRule . /frontend/web/index.php [L]

    # if a directory or a file of the backend application exists, use the request directly
    RewriteCond %{REQUEST_URI} ^/backend/web
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # otherwise forward the request to index.php
    RewriteRule . /backend/web/index.php [L]

    RewriteCond %{REQUEST_URI} \.(htaccess|htpasswd|svn|git)
    RewriteRule \.(htaccess|htpasswd|svn|git) - [F]
</IfModule>
</code>

Also look [[https://devreadwrite.com/posts/htaccess-for-yii-2-basic|here]] for the basic template.

Create an .htaccess file in the root folder, then add the following
<code>
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
 
RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
RewriteRule ^js/(.*)$ web/js/$1 [L]
RewriteRule ^images/(.*)$ web/images/$1 [L]
RewriteRule (.*) /web/$1
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php
</code>

In the /web directory, create another ''.htaccess'' file with
<code>
RewriteEngine On RewriteBase /
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
 
RewriteRule . index.php
</code>

Edit the /config/web.php\\
add
<code>
    'baseUrl' => ''
</code>
under the ''CookieValidationKey'' line,
uncomment the ''UrlManager'' bit and add
<code>
        '' => 'site/index',                                
        '<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
</code>
in the rules square brackets

==== Simpler htaccess for basic ====
In the ''root'' folder
<code>
RewriteEngine On
RewriteRule ^$ web/
RewriteCond %{REQUEST_FILENAME} !–f
RewriteCond %{REQUEST_FILENAME} !–d
RewriteRule ^(.*)$ web/$1
</code>

In the web folder
<code>
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
</code>

Edit the /config/web.php and add
<code>
    'baseUrl' => ''
</code>
under the ''CookieValidationKey'' line or\\

and add to the beginning of ''web.php''
<code php>
$baseUrl = str_replace('/web', '', (new \yii\web\Request)->getBaseUrl());
</code>


Gii is now ''http://localhost/web/gii'' or ''http://localhost/gii''


Back to [[yii|Yii Main Index]]