Enable clean URL in Yii2

后端 未结 11 1021
攒了一身酷
攒了一身酷 2020-11-27 12:36

How can I enable clean urls in Yii2. I want to remove index.php and \'?\' from url parameters. Which section needs to be edited in Yii2 for that?

相关标签:
11条回答
  • 2020-11-27 12:44

    Step1: in project config/main.php eg: frontend/config/main.php

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

    Step2: create .htaccess file inset web folder eg: frontend/web

    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
    
    #php_flag  display_errors        on
    #php_value error_reporting       2039
    
    0 讨论(0)
  • 2020-11-27 12:45

    I got it working in yii2. Enable mod_rewrite for Apache. For basic template do the following: Create a .htaccess file in web folder and add this

    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
    

    Then inside config folder, in web.php add to components

    'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),
    ],
    

    In the case of advanced template create the .htaccess files inside backend/web and frontend/web folders and add urlManager component inside common/config/main.php

    0 讨论(0)
  • 2020-11-27 12:52

    on nginx configure like that

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    
    0 讨论(0)
  • 2020-11-27 12:55

    First important point is that

    Module_Rewrite is enabled on your server(LAMP,WAMP,XAMP..etc) For do URL rewiring in yii2 framework Create one .htaccess file and put in /web folder

    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
    

    Second step

    Config folder common/config/main-local.php add to components array

    'urlManager' => [
       'class' => 'yii\web\UrlManager',
       // Disable index.php
       'showScriptName' => false,
       // Disable r= routes
       'enablePrettyUrl' => true,
       'rules' => array(
          '<controller:\w+>/<id:\d+>' => '<controller>/view',
          '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
          '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
       ),
    ],
    
    0 讨论(0)
  • 2020-11-27 12:58

    Just to add to this discussion - I've just installed Yii2, and it includes the following commented-out code in config/web.php:

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

    If you add the .htaccess file in the accepted answer, then just uncomment the above, pretty URLs will work (I have no idea what the "rules" in the accepted answer are for, but everything seems to work without them).

    0 讨论(0)
  • 2020-11-27 13:01

    Step-by-step instruction

    Step 1

    At the root of the project add a .htaccess with the following content:

    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
    

    Step 2

    In the folder /web add a .htaccess file with the following content:

    RewriteEngine On RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule . index.php
    

    Step 3

    In the file /config/web.php in element components of array add folowing code:

    'request' => [
        // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
        'cookieValidationKey' => 'yYy4YYYX8lYyYyQOl8vOcO6ROo7i8twO',
        'baseUrl' => ''
    ],
    
    //...
    
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '' => 'site/index',                                
            '<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
        ],
    ],
    

    Done..

    0 讨论(0)
提交回复
热议问题