Php pseudo-static writing (apache pseudo-static rules)

Source: Internet
Author: User
Tags explode modifier regular expression vars

The php program implements the pseudo static url as follows.

The code is as follows: Copy code

<? Php
// Pseudo-static method 1
// Localhost/php100/test. php? Id | 1 @ action | 2
$ Php2Html_FileUrl = $ _ SERVER ["REQUEST_URI"];
Echo $ Php2Html_FileUrl. "<br>"; // php100/test. php? Id | 1 @ action | 2
$ Php2Html_UrlString = str_replace ("? "," ", Str_replace ("/"," ", strrchr ($ Php2Html_FileUrl ,"/"),"? ")));
Echo $ Php2Html_UrlString. "<br>"; // id | 1 @ action | 2
$ Php2Html_UrlQueryStrList = explode ("@", $ Php2Html_UrlString );
Print_r ($ Php2Html_UrlQueryStrList); // Array ([0] => id | 1 [1] => action | 2)
Echo "<br> ";
Foreach ($ Php2Html_UrlQueryStrList as $ Php2Html_UrlQueryStr)
{
$ Php2Html_TmpArray = explode ("|", $ Php2Html_UrlQueryStr );
Print_r ($ Php2Html_TmpArray); // Array ([0] => id [1] => 1); Array ([0] => action [1] => 2)
Echo "<br> ";
$ _ GET [$ Php2Html_TmpArray [0] = $ Php2Html_TmpArray [1];
}
// Echo 'false static: $ _ GET variable <br/> ';
Print_r ($ _ GET); // Array ([id | 1 @ action | 2] => [id] => 1 [action] => 2)
Echo "<br> ";
Echo "Echo $ _ GET [id]. "<br>"; // 1
Echo $ _ GET [action]; // 2
?>
 
<? Php
// Pseudo-static method 2
// Localhost/php100/test. php/1/2
$ Filename = basename ($ _ SERVER ['script _ name']);
Echo $ _ SERVER ['script _ name']. "<br>"; // php100/test. php
Echo $ filename. "<br>"; // test. php
If (strtolower ($ filename) = 'Test. Php '){
If (! Empty ($ _ GET [id]) {
$ Id = intval ($ _ GET [id]);
Echo $ id. "<br> ";
$ Action = intval ($ _ GET [action]);
Echo $ action. "<br> ";
} Else {
$ Nav = $ _ SERVER ['request _ URI '];
Echo "1:". $ nav. "<br>"; // php100/test. php/1/2
$ Script = $ _ SERVER ['script _ name'];
Echo "2:". $ script. "<br>"; // php100/test. php
$ Nav = ereg_replace ("^ $ script", "", urldecode ($ nav ));
Echo $ nav. "<br>"; // 1/2
$ Vars = explode ("/", $ nav );
Print_r ($ vars); // Array ([0] => [1] => 1 [2] => 2)
Echo "<br> ";
$ Id = intval ($ vars [1]);
$ Action = intval ($ vars [2]);
}
Echo $ id. '&'. $ action;
}
?>
 
<? Php
// Pseudo-static method 3
Function mod_rewrite (){
Global $ _ GET;
$ Nav = $ _ SERVER ["REQUEST_URI"];
Echo $ nav. "<br> ";
$ Script_name = $ _ SERVER ["SCRIPT_NAME"];
Echo $ script_name. "<br> ";
$ Nav = substr (ereg_replace ("^ $ script_name", "", urldecode ($ nav), 1 );
Echo $ nav. "<br> ";
$ Nav = preg_replace ("/^. ht (m) {1} (l) {0, 1} $/", "", $ nav); // The end of this sentence is .htmlor .htm.
Echo $ nav. "<br> ";
$ Vars = explode ("/", $ nav );
Print_r ($ vars );
Echo "<br> ";
For ($ I = 0; $ I <Count ($ vars); $ I ++ = 2 ){
$ _ GET ["$ vars [$ I]"] = $ vars [$ I + 1];
}
Return $ _ GET;
}
Mod_rewrite ();
$ Year = $ _ GET ["year"]; // The result is 2006'
Echo $ year. "<br> ";
$ Action = $ _ GET ["action"]; // The result is '_ add'
Echo $ action;
?>
 
<? Php
// Pseudo-static method 4
// Use the server variable to obtain the PATH_INFO information. In this example, it is/, 8630.html, that is, the part after the execution script name.
If (@ $ path_info = $ _ SERVER ["PATH_INFO"]) {
// Regular expression matching parameters
If (preg_match ("// (d1_success, (d1_success, (d1_0000.html/si", $ path_info, $ arr_path )){
$ Gid = intval ($ arr_path [1]); // get the value 1
$ Sid = intval ($ arr_path [2]); // get the value 100
$ Softid = intval ($ arr_path [3]); // get the value 8630
} Else die ("Path: Error! ");
// Equivalent to soft. php? Gid = 1 & sid = 100 & softid = 8630
} Else die ('path: Nothing! ');
?>

If you have Server permissions, I think apache % CE % B1 % BE % B2 % CC % AC/"target =" _ blank "> apache pseudo static

1. Apache configuration:

Go to the/etc/httpd/conf/directory and open the httpd. conf file.
Enable rewrite
# LoadModule rewrite_module modules/mod_rewrite.so remove the previous #
Enable. htaccess
AllowOverride None: AllowOverride All

II. Rewrite writing

We cannot modify the configuration file on the server. In most cases, we need to create a. htaccess file under the root directory of the website.

The code is as follows: Copy code
RewriteEngine on // start the rewrite engine
RewriteRule ^/index ([0-9] * pai.html $/index. php? Id = $1 // "([0-9] *)" indicates that the range (. *) indicates all, the same below.
RewriteRule ^/index ([0-9] *)/$/index. php? Id = $1 [R] // virtual directory

3. mod_rewrite rule modifier

1) R forces external redirection
2) F disables the URL and returns the 403HTTP status code.
3) if G forces the URL to be GONE, the response HTTP status code is returned.
4) P enforces proxy forwarding.
5) L indicates that the current rule is the last rule, and the rule is overwritten after analysis is stopped.
6) N re-run the rewrite process from the first rule.
7) C. Associate with the next rule
If the rule matches, it is processed normally. The following modifier is invalid.
8) T = MIME-type (force MIME type) force MIME type
9) NS is only used for non-internal subrequests
10) the NC is case insensitive.
11) QSA append request string
12) NE does not escape special characters in the output. % 3d $1 is equivalent to = $1.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.