Yii Framework URL Path simplification

Source: Internet
Author: User
Tags yii

The access address of the YII framework, if not simplified, can be cumbersome, and the simplified address general format is as follows:

Http://localhost:80/test/index.php?r=xxx/xxx/xxx

It can be more complicated with parameters:

Http://localhost:80/test/index.php?r=xxx/xxx/xxx&param1=XXX&param2=XXX


So how do we beautify it:

1. In the Config folder under the main.php file, find the configuration of Urlmanager, modified as follows:

' Urlmanager ' =>array (
' Urlformat ' => ' path ',
' Rules ' =>array (
' <controller:\w+>/<id:\d+> ' => ' <controller>/view ',
' <controller:\w+>/<action:\w+>/<id:\d+> ' => ' <controller>/<action> ',
' <controller:\w+>/<action:\w+> ' => ' <controller>/<action> ',
' <controller:\w+>/<action:\w+> ' => ' <controller>/<action> ',
),
),


Now to visit your project, you will find that your project URL becomes:

Http://localhost:80/test/index.php/XXX/XXX

2. This and not enough, we want to continue to simplify, the index.php also removed. Continue to modify the main.php file,

' Urlmanager ' =>array (
' Urlformat ' => ' path ',
' Showscriptname ' =>false,//Remove index.php
' Urlsuffix ' => '. html ',//Plus. html
' Rules ' =>array (
' <controller:\w+>/<id:\d+> ' => ' <controller>/view ',
' <controller:\w+>/<action:\w+>/<id:\d+> ' => ' <controller>/<action> ',
' <controller:\w+>/<action:\w+> ' => ' <controller>/<action> ',
' <controller:\w+>/<action:\w+> ' => ' <controller>/<action> ',
),
),

After this modification, index.php disappeared, but found that part of the URL error. How to solve it.

3. Add the. htaccess file for access to the project. The contents are as follows:

<ifmodule mod_rewrite.c>

Options +followsymlinks
Indexignore */*
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

</IfModule>



This entry will automatically access the index.php file, the URL will not be garbled.


The last simplified URL is as follows:

Http://localhost:80/test/XXX/XXX

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.