Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand php url routing

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to understand php url routing", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand php url routing.

What is the routing mechanism of php

1. The routing mechanism is to extract the corresponding parameters of the system from a specific form of URL structure. For example, http://main.test.com/article/1 where: / article/1->? _ m=article&id=1.

2. Then convert the URL with corresponding parameters into a specific form of URL structure, which is the reverse process of the above process.

Second, the URL routing mode of PHP

Generally speaking, it is: get path information-> process path information

URL routing method:

The first way is to map through url parameters, usually two parameters, which represent the controller class and methods, such as index.php?c=index&m=index mapping to the index method of the index controller.

The second is through url-rewrite. The advantage of this is that you can map other suffixes that do not end with php. Of course, you can also achieve the first way through rewrite, but it is also common to use rewrite only. You generally need to configure apache or nginx.

Rewrite rule

The copy code is as follows:

RewriteEngine On

RewriteBase /

RewriteRule ^ index\ .php $- [L]

RewriteCond% {REQUEST_FILENAME}!-f

RewriteCond% {REQUEST_FILENAME}!-d

RewriteRule. / index.php [L]

The third is through the pathinfo way, the so-called pathinfo, is shaped like this url. When dealing with this url, xxx.com/index.php/c/index/aa/cc,apache enters the following part of index.php into the environment variable $_ SERVER ['PATH_INFO'], which is equal to / c/index/aa/cc. Then our router can analyze the string by parsing it, and where the parameters are put in the latter part varies according to each framework.

Third, a simple PHP routing implementation

3.1 modify the htaccess file

Write the rewrite file that comes with the server apache or IIS, and import the URL structure into the specified file such as: index.php.

Opening the rewrite:htaccess file is a configuration file in the Apache server, which is responsible for the configuration of web pages in the relevant directories. To enable .htaccess, you need to modify apache/conf/httpd.conf, enable AllowOverride, and use AllowOverride to restrict the use of specific commands.

The copy code is as follows:

Options FollowSymLinks

AllowOverride None

Change to

The copy code is as follows:

Options FollowSymLinks

AllowOverride All

Then I wrote a rewrite like this:

The copy code is as follows:

RewriteEngine on # rewriteengine is the rewrite engine switch on is on, off is off

# RewriteCond $1! ^ (index.php\ .php | images | robots\ .txt)

RewriteRule ([a-zA-Z] {1,})-([0-9] {1,}) .html$ sharexie/test.php?action=$1&id=$2

# ([a-zA-Z] {1,})-([0-9] {1,}) .html $is the rule, sharexie/test.php?action=$1&id=$2 is the format to replace, $1 represents the value matched by the first parenthesis, and $2 represents the second.

The above code is to import the URL structure into sharexie/test.php. Just save these as .htaccess files and put them in the root directory of the site.

Test.php

The copy code is as follows:

All right, let's type in the browser now:

127.0.0.1/view-12.html

The output is:

Your Action is: view

Your ID is: 12

1. Explain RewriteRule:

RewriteRule is a rewriting rule that supports regular expressions. The above ([0-9] {1,}) refers to a number, and $is a closing flag, indicating that it ends with a number!

2. RewriteRule configuration parameters

1) R forces external redirection

2) F disable URL and return 403HTTP status code.

3) G forces URL to be GONE and returns 410HTTP status code.

4) P forcibly use proxy forwarding.

5) L indicates that the current rule is the last rule, stop analyzing the rewriting of the rule after.

6) N re-start the rewrite process from the first rule.

7) C associates with the next rule 8) T=MIME-type (force MIME type) mandatory MIME type

9) NS is only used for non-internal sub-requests

10) NC is not case sensitive

11) QSA append request string

12) NE is not in output escape special character\% 3d$1 is equivalent to = $1

For example:

1. Set xianglc to index.php?c=myuser&m=itime&domain=xianglc

The copy code is as follows:

RewriteRule ^ ([a-zA-Z0-9]) {6Jing 20} /? $index.php?c=myuser&m=itime&domain=$0 [L]

2. # RewriteRule ^ / index.html$ / 1.php [L]

The copy code is as follows:

RewriteRule ^ / index- (. *?) $9&a=$1&b=$2&c=$3&d=$4&e=$5&f=$6&g=$7&h=$8 [CJN]

RewriteRule ^ (. *?)-(. *?). Html (. *?) $/ 1.php?$7&i=$1&j=$2&k=$3&l=$4&m=$5&n=$6 [QSA,L,NC]

3.2A routing parser for parsing rules, matching and transforming URL.

First, transfer all the links to index.php, route distribution in index.php, and assign them to the functions in the corresponding class files according to classes and methods. Take out the part after the www.xx.com/ in the URL with $_ SERVER ['REQUEST_URI'] and distinguish it into class and mothod and the value of the parameter key= > value according to the relevant rules. Finally, include the class file and execute the functions in it. Examples are as follows:

The copy code is as follows:

At this point, I believe you have a deeper understanding of "how to understand php url routing". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report