Skip to content

Instantly share code, notes, and snippets.

@fixable11
Last active September 22, 2018 13:06
Show Gist options
  • Save fixable11/5e074975cfa515043b41c4e2d9695fd0 to your computer and use it in GitHub Desktop.
Save fixable11/5e074975cfa515043b41c4e2d9695fd0 to your computer and use it in GitHub Desktop.
Yii2 htaccess

#Yii2 basic htaccess

Demo

Video Demo

Video Demo

a. Automatic setting index.php

This method created .htaccess files automatically and clear self.

b. Manual setting

1. step

create file {root}/.htaccess:

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
</IfModule>
 
<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_URI} ^/.*
    RewriteRule ^(.*)$ web/$1 [L]

    RewriteCond %{REQUEST_URI} !^/web/
    RewriteCond %{REQUEST_FILENAME} !-f [OR]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ web/index.php
</IfModule> 

2. step

create file {root}/web/.htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

3. step

update file {root}/config/web.php:

'request' => [
    'baseUrl' => '',
],
'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        '' => 'site/index',
        '<action>'=>'site/<action>',
    ],
],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment