Php pseudo-static writing, apache pseudo-static rules

Source: Internet
Author: User
Tags vars
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 we want to implement real pseudo-static data, we can use apacheRewrite pseudo-static data... 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.

The php program implements pseudo-static URLs. the pseudo-static method 1 is as follows:

 "; // 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 'pseudo 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?>

Php programs implement pseudo-static URLs. Method 2:

 "; // /php100/test.phpecho $filename . "
"; // test.phpif (strtolower($filename) == 'test.php') { if (!emptyempty($_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;}?>

Php programs implement pseudo-static URLs. Method 3:

 "; $ 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 <Count ($ vars); $ I + = 2) {$ _ GET ["$ vars [$ I]"] = $ vars [$ I + 1];} return $ _ GET;} mod_rewrite (); $ year = $ _ GET ["year"]; // The result is '000000' echo $ year."
"; $ Action = $ _ GET [" action "]; // The result is '_ add' echo $ action;?>

Php implements pseudo-static URLs. Method 4:

 

If you have Server permissions, I think apache pseudo-static is still used.

1. Apache configuration:

Go to the/etc/httpd/conf/directory, open the httpd. conf file, and enable rewrite:

# LoadModule rewrite_module modules/mod_rewrite.so remove the previous #

Enable. htaccess

AllowOverride None: AllowOverride All

II. Rewrite statement:

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.


Article URL:

Reprint ^ at will, but please attach the tutorial address.

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.