First, what is route rewriting
Original HTTP access address: WWW.TEST.COM/INDEX.PHP?R=POST/VIEW&ID=100 indicates that the request will be handled by Postcontroller's Actionview.
The rewritten HTTP access address www.test.com/post/view/id/100 such a link looks simple and beautiful for the user is more friendly. At the same time, also more suitable for search engine appetite, is said to be one of the means of SEO.
Second, Apache Route rewrite
(i) Open Apache rewrite module
1. Open the hhttpd.conf of Apache config to remove the # number #LoadModule Rewrite_module modules/mod_rewrite.so
2. Restart the Apache server to
(ii) Virtual host configuration
1. Open the apache\conf\vhosts.conf configuration path
2. Modify the configuration to read as follows:
127.0. 0.1:> ServerName Test-yii2.com documentroot F:/wamp64/www/yii2/ frontend/Web <directory "f:/wamp64/www/yii2/frontend/web" > Options +indexes +includes +followsymlinks +multiviews allowoverride all Require all granted Rewriteengine on # If the requested file or directory is real, Access Rewritecond%{request_filename} directly! -F rewritecond%{request_filename}! -D # If the request is not a real file or directory, distribute the request to index.php rewriterule. index.php </directory></ Virtualhost>
3. Restart Apache
(iii) there is another way to configure the Apache server without restarting, when the change took effect.
1. Add the. htaccess code below the "F:/wamp64/www/yii2/frontend/web" directory:
rewriteengine on # If the requested file or directory is real, Access Rewritecond%{request_filename} directly! -F rewritecond%{request_filename}! -D # If the request is not a real file or directory, distribute the request to index.php rewriterule. index.php
(d) Yii configuration
1. Under main.php components
, add the following configuration to this array:
' Urlmanager ' = [ ' Enableprettyurl ' =true,//turn on Beautify URL configuration, turn off by default' Enablestrictparsing ' =false,//Strict parsing is not enabled and is not enabled by default. If set to true, the rules rule must be established and the path must conform to more than one rule to allow access' Showscriptname ' =false,//Hide index.php' Rules ' = [ //Http://frontend.com/site/index rewrite to Http://frontend.com/site' <controller:\w+>/' = ' <controller>/index ',//http://frontend.com/site/view?id=1 rewrite to HTTP://FRONTEND.COM/SITE/1' <controller:\w+>/<id:\d+> ' = ' <controller>/view ',//http://frontend.com/site/ceshi?id=123 rewrite to http://frontend.com/site/ceshi/123' <controller:\w+>/<action:\w+>/<id:\d+> ' = ' <controller>/<action> ', ] ],
Yii2 Apache + Nginx Routing rewrite