开启路径美化以后

I got it working in yii2. Enable mod_rewrite for Apache. For basic template do the following: Create an .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

shareimprove this answer
edited Nov 9 '15 at 8:05
answered Oct 24 '14 at 18:25

user7282
2,99732455
  	 	
The rules you applied helped me, I tried without rules but always got error. – ankitr Mar 5 '15 at 3:57
  	 	
Ok, it seems this one is better. Just remember, that .htaccess file thing is only applicable if you are using apache - it's different with nginx and other web servers. I am deleting the second (mine) answer. – tebazil Mar 12 '15 at 12:33 
3	 	
Sometimes controllers and actions have a dash - character. I had to change \w+ to [\w\-]+ to get it to work with those. – cornernote Jul 15 '15 at 10:34
  	 	
Thank you for adding the answer for an advanced template – Melle Dijkstra Jun 18 '16 at 10:24 
  	 	
not working for advanced template – Yasin Patel Oct 11 '16 at 10:30
add a comment

up vote
10
down vote
For me, the problem was:

Missing .htaccess in the web folder just like mentioned above.
The AllowOverride directive was set to None, which disabled URL rewrites. I changed it to All and now pretty URLs work nicely.
<Directory "/path/to/the/web/directory/">
  Options Indexes 
  FollowSymLinks MultiViews 
  AllowOverride All 
  Require all granted
</Directory>
shareimprove this answer
answered May 1 '15 at 17:07

pkout
2,67611834
  	 	
FollowSymLinks MultiViews this line made syntax error in the config file for me and I had to make it like this Options Indexes FollowSymLinks – Amir Aug 29 '16 at 12:52
add a comment
up vote
8
down vote
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>',
        ),
        ],
shareimprove this answer
edited Apr 27 '15 at 13:58

Brad Larson♦
152k36337500
answered Apr 6 '15 at 23:02

Sijo Thomas Maprayil
88276
1	 	
When you have hyphens/dashes in the controller and action names you need to replace \w+ with [\w\-]+. – TheStoryCoder Jul 4 '16 at 19:54 
add a comment
up vote
2
down vote
on nginx configure like that

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}
shareimprove this answer
answered Aug 6 '16 at 16:31

rakhmatov
664
add a comment
up vote
1
down vote
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).

shareimprove this answer

  

posted @ 2017-02-20 13:48  Yeah,程序猿  阅读(189)  评论(0编辑  收藏  举报