Yii2 Apache + Nginx Routing rewrite

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.