PHP implements URL pseudo-static

Source: Internet
Author: User
Tags explode

The static URL, I generally with the help of apache,nginx,htaccess and so on, very little PHP to achieve the static URL, using PHP to achieve static more cumbersome, so on the individual is not recommended to use PHP to achieve URL static

One, the principle of the static implementation of the URL

1, through the program to convert the dynamic URL to a static URL, the conversion method preferably is a unified interface. Put the static URL into the HTML, so that we see through the page is a static URL.

2,apache or others, load the rewrite module and add rewrite rules. So when we click on the static URL in the page, we can turn to the correct URL. Although the PHP file is executed, the browser's address bar displays the static URL.

If you use PHP to implement the URL static, but also to maintain the address bar inside the static URL, this is more troublesome, the following is a simple example to give.

Two, convert the dynamic URL to a static URLView copy print? <?php//Convert URL to static URLfunctionUrl_rewrite ($file, $params =Array(), $html = "", $rewrite = True) {if($rewrite) {//Development phase is not rewrite, where the development, the $rewrite = False $url = ($file = = ' index ')? '' : '/' . $file;if(!emptyEmpty($params) && Is_array ($params)) {$url. = '/'. Implode ('/', $params); }if(!emptyEmpty($html))     {$url. = '. ' $html; }     }Else{$url = ($file = = ' index ')? '/' : '/' . $file;if(Substr ($url,-4)!= '. php ' && $file!= ' index ')     {$url. = '. php '; }if(!emptyEmpty($params) && Is_array ($params)) {$url. = '? '. Http_build_query ($params); }     } return$url; echo url_rewrite (' test.php ',Array(' class ' => ' User ', ' act ' => ' check ', ' name ' => ' Tank ')); $rewrite = False, the following/test.php?class=user&act=check&name=tank echo url_rewrite (' test.php ') is displayed,Array(' class ' => ' User ', ' act ' => ' check ', ' name ' => ' Tank ')); $rewrite = True, the following/test.php/user/check/tank echo url_rewrite (' Test ') is displayed,Array(' class ' => ' User ', ' act ' => ' check ', ' name ' => ' Tank ')); $rewrite = True, the following/test/user/check/tank echo url_rewrite (' Test ') is displayed,Array(' class ' => ' User ', ' act ' => ' check ', ' name ' => ' tank '), ' HTML '); $rewrite = True, the following/test/user/check/tank.html?> <a href= is displayed./test3<?php echo url_rewrite (' test.php ', a Rray (' class ' => ' User ', ' act ' => ' check ', ' name ' => ' Tank '));? > ">test</a>

<?php//Convert URL to static URL function url_rewrite ($file, $params = Array (), $html = "", $rewrite = True) {if ($rewrite) { Development phase is not rewrite, where the development, the $rewrite = False $url = ($file = = ' index ')? '' : '/' .
 $file;
 if (!empty ($params) && Is_array ($params)) {$url. = '/'. Implode ('/', $params);
 } if (!empty ($html)) {$url. = '. ' $html; } else {$url = ($file = = ' index ')? '/' : '/' .

 $file;
 if (substr ($url,-4)!= '. php ' && $file ' index ') {$url. = '. php ';
 } if (!empty ($params) && Is_array ($params)) {$url. = '? '. Http_build_query ($params);
} return $url;
echo url_rewrite (' test.php ', Array (' class ' => ' User ', ' act ' => ' check ', ' name ' => ' Tank ')); $rewrite = False, displays the following/test.php?class=user&act=check&name=tank echo url_rewrite (' test.php ', Array ('
Class ' => ' User, ' act ' => ' check ', ' name ' => ' tank '); $rewrite = True, the following/test.php/user/check/tank echo url_rewrite (' Test ', Array (' class ' => ' User ', ' act ' => ') is displayedCheck ', ' name ' => ' Tank ')); $rewrite = True, the following/test/user/check/tank echo url_rewrite (' Test ', Array (' class ' => ' User ', ' act ' => ' check ') is displayed
, ' name ' => ' tank '), ' HTML '; $rewrite = True, the following/test/user/check/tank.html?> <a href= is displayed./test3<?php echo url_rewrite (' test.php ', Array (' class ' => ' User ', ' act ' => ' check ', ' name ' => ' Tank '));?
 > ">test</a>

It simply wrote a method to convert the dynamic URL to a static URL, the page will produce a link to the following: view copy print? <a href= "./test3/test.php/user/check/tank" >test</a>

<a href= "./test3/test.php/user/check/tank" >test</a>

If you click here, you will certainly report 404 errors, because the root cannot find tank this directory. The difficulty is here, so we need to assign a PHP file to the directories and files that we can't find. This will take advantage of Apache,nginx, or htaccess.

Three, specify a unified portal view copy print? Rewritecond%{request_filename}!-f//Cannot find file Rewritecond%{request_filename}!-d//directory rewriterule not found. /test3/index.php [L]

Rewritecond%{request_filename}!-f//Unable  to find file
rewritecond%{request_filename}!-d  //directory
not hit Rewriterule. /test3/index.php [L]

Whether you're implementing it in a. htaccess, or in a configuration file like Apache, it's OK. What does the above three words mean if the directory is not found to go to the index.php file, if the file is not found, also go to index.php.

This done, when we visit Http://localhost/test3/test.php/User/check/tank time, will be transferred to index.php , since we know that PHP file, it is good to do.

The following are all done in Http://localhost/test3/test.php/User/check/tank this way, and all the other ways are similar.

Four, index.php file View copy print? <?php       $filename  = $_server[' Request_uri '];  //requested URL      /** Request URL, "/test3/test.php/user/check/tank"    * test.php  to go to php file    *  User  is class name    * check  is the method name in class    * tank  is to be uploaded to check parameters * *       Preg_match ("/(/w+/.php)/", $filename, $match);     //find php file name        $array  =  explode ('/', $filename);        // Split the static URL       $key   =  array_keys ($array, $match [0]);   // Get the corresponding subscript array  ( [0] => 2 )    $file _array = array_slice of the file ($array , 0, $key [0]+1);  //array  ( [0] => [1] => test3 [2] = > test.php )    $param _array = array_slice ($array, $key [0]+1);    //array  ( [0] => User [1] => check [2] =>  tank )         $file _path = implode ('/', $file _array);      if($array [$key [0]]!= "index.php") {include_once($array [$key [0]]); Package request URL in the PHP file, here is test.php}if(Class_exists ($param _array[0]) {//Judge test.php This file has user this class $obj =New$param _array[0];if(Method_exists ($obj, $param _array[1])  {//Judge whether the user this class has check this method $obj-> $param _array[1] ($param _array[2]); Call this method, the result is (my name is called Tank)}}?>

<?php $filename = $_server[' Request_uri ']; Requested URL/** requested URL, "/test3/test.php/user/check/tank" * test.php php file to go * User is class name * check is the method name in class * tank is to    The parameters to the check/Preg_match ("/(/w+/.php)/", $filename, $match);       Find the PHP filename $array = explode ('/', $filename);  The static URL is segmented $key = Array_keys ($array, $match [0]);  Get the corresponding subscript array ([0] => 2) $file _array = array_slice ($array, 0, $key [0]+1);   Array ([0] => [1] => test3 [2] => test.php) $param _array = Array_slice ($array, $key [0]+1);

Array ([0] => User [1] => check [2] => tank) $file _path = implode ('/', $file _array);            if ($array [$key [0]]!= "index.php") {include_once ($array [$key [0]]); Package request URL in the PHP file, here is test.php} if (Class_exists ($param _array[0)) {//Judge test.php This file has no user this class $obj = new $pa
 RAM_ARRAY[0]; if (Method_exists ($obj, $param _array[1])) {//Determine if there is a check in user this class $obj-> $param _array[1] ($param _array[2  ]); Call this method, the result is (my name is called Tank)}}?>

Five, test.php file view copy print?     <?php class User { public function Check ($name) {echo "My name is called". $name; }}?>

<?php

class User {public
 function check ($name) {
 echo "My name is called". $name;
 }
}
? >

Here, when we visit Http://localhost/test3/test.php/User/check/tank this web site,

The results are as follows: My name is tank, and the address bar still remains static.

here through PHP, we have completed a simple URL rewrite process. In fact, with the help of. htaccess, or Apache, but the rewrite rules are not in. htaccess, or Apache. If you completely use PHP to achieve the static URL, personal feeling, impossible.

Reprint Please specify
Author: Undersea Eagle
Address:
http://blog.51yip.com/php/1219.html

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.