Php pseudo-static writing (apache pseudo-static rules)-PHP source code

Source: Internet
Author: User
In php, we can directly use php to process URLs for pseudo-static operations. However, this pseudo-static mode is not a standard and can be said to look like a commodity, but it is quite comfortable, of course, to implement real pseudo-static data, we can use the apacheRewrite pseudo-static module for example. Let's take a look at it. In php, we can directly use php to process URLs for pseudo-static operations. However, this pseudo-static mode is not a standard and can be said to look like a commodity, but it is quite comfortable, of course, if you want to implement real pseudo-static data, we can use apache Rewrite pseudo-static modules for instances. Let's take a look at them.

Script ec (2); script

The php program implements the pseudo static url as follows.

The Code is as follows:

// Pseudo-static method 1
// Localhost/php100/test. php? Id | 1 @ action | 2
$ Php2Html_FileUrl = $ _ SERVER ["REQUEST_URI"];
Echo $ Php2Html_FileUrl ."
"; // Php100/test. php? Id | 1 @ action | 2
$ Php2Html_UrlString = str_replace ("? "," ", Str_replace ("/"," ", strrchr ($ Php2Html_FileUrl ,"/"),"? ")));
Echo $ Php2Html_UrlString ."
"; // Id | 1 @ action | 2
$ Php2Html_UrlQueryStrList = explode ("@", $ Php2Html_UrlString );
Print_r ($ Php2Html_UrlQueryStrList); // Array ([0] => id | 1 [1] => action | 2)
Echo"
";
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"
";
$ _ GET [$ Php2Html_TmpArray [0] = $ Php2Html_TmpArray [1];
}
// Echo 'false static: $ _ GET variable
';
Print_r ($ _ GET); // Array ([id | 1 @ action | 2] => [id] => 1 [action] => 2)
Echo"
";
Echo "";
Echo $ _ GET [id]."
"; // 1
Echo $ _ GET [action]; // 2
?>

// Pseudo-static method 2
// Localhost/php100/test. php/1/2
$ Filename = basename ($ _ SERVER ['script _ name']);
Echo $ _ SERVER ['script _ name']."
"; // Php100/test. php
Echo $ filename ."
"; // Test. php
If (strtolower ($ filename) = 'test. php '){
If (! Empty ($ _ GET [id]) {
$ Id = intval ($ _ GET [id]);
Echo $ id ."
";
$ Action = intval ($ _ GET [action]);
Echo $ action ."
";
} Else {
$ Nav = $ _ SERVER ['request _ URI '];
Echo "1:". $ nav ."
"; // Php100/test. php/1/2
$ Script = $ _ SERVER ['script _ name'];
Echo "2:". $ script ."
"; // Php100/test. php
$ Nav = ereg_replace ("^ $ script", "", urldecode ($ nav ));
Echo $ nav ."
"; // 1/2
$ Vars = explode ("/", $ nav );
Print_r ($ vars); // Array ([0] => [1] => 1 [2] => 2)
Echo"
";
$ Id = intval ($ vars [1]);
$ Action = intval ($ vars [2]);
}
Echo $ id. '&'. $ action;
}
?>

// Pseudo-static method 3
Function mod_rewrite (){
Global $ _ GET;
$ Nav = $ _ SERVER ["REQUEST_URI"];
Echo $ nav ."
";
$ Script_name = $ _ SERVER ["SCRIPT_NAME"];
Echo $ script_name ."
";
$ Nav = substr (ereg_replace ("^ $ script_name", "", urldecode ($ nav), 1 );
Echo $ nav ."
";
$ Nav = preg_replace ("/^. ht (m) {1} (l) {0, 1} $/", "", $ nav); // the end of this sentence is .htmlor .htm.
Echo $ nav ."
";
$ Vars = explode ("/", $ nav );
Print_r ($ vars );
Echo"
";
For ($ I = 0; $ I $ _ GET ["$ vars [$ I]"] = $ vars [$ I + 1];
}
Return $ _ GET;
}
Mod_rewrite ();
$ Year = $ _ GET ["year"]; // The result is 2006'
Echo $ year ."
";
$ Action = $ _ GET ["action"]; // The result is '_ add'
Echo $ action;
?>

// 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:
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.

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.