Convert URLs to static URLs
- function Url_rewrite ($file, $params = Array (), $html = "", $rewrite = True) {
- if ($rewrite) {//development phase is not rewrite, where the development of the time, put $rewrite = False
- $url = ($file = = ' index ')? '' : '/' . $file;
- if (! empty ($params) && Is_array ($params)) {
- $url. = '/'. Implode ('/', Array_slice ($params, 0, 2));
- $param = Array_slice ($params, 2);
- foreach ($param as $key = = $value) {
- $url. = '/'. $key. '/' . UrlEncode ($value);
- }
- }
- 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 ', Array (' class ' + ' User ', ' act ' = ' check ', ' name ' = ' tank ', ' page ' =>5);
";
- $rewrite = False, the following/test.php?class=user&act=check&name=tank is displayed
echo url_rewrite (' test.php ', Array (' class ' = ' + ' User ', ' act ' = ' check ', ' name ' = ' tank ')); ";
- $rewrite = True, the following/test.php/user/check/tank are displayed
- echo url_rewrite (' Test ', Array (' class ' + ' User ', ' act ' = ' check ', ' name ' = ' tank '));
";
- $rewrite = True, the following/test/user/check/tank are displayed
echo url_rewrite (' Test ', Array (' class ' = ' + ' User ', ' act ' = ' check ', ' name ' = ' tank '), ' HTML '); ";
- $rewrite = True, the following/test/user/check/tank.html are displayed
- ?>
Copy Code"User", ' act ' = ' check ', ' name ' = ' tank ')); > ">test The above to convert the dynamic URL into a static URL, the page will generate links as follows: Test if you click directly, will certainly report 404 error, because the root can not find tank this directory. You need to specify a PHP file for directories and files that you cannot find. This need to use Apache,nginx, or htaccess and so on. Third, specify a unified portal
- Rewritecond%{request_filename}!-f//File not found
- Rewritecond%{request_filename}!-d//cannot hit directory
- Rewriterule. /test3/index.php [L]
Copy CodeCode Description: If you cannot find the directory to go to the index.php file, if you cannot find the file, go to index.php. When you visit Http://localhost/test3/test.php/User/check/tank, you will be transferred to index.php. The following are all manipulated in such a way that Http://localhost/test3/test.php/User/check/tank is overridden. IV. index.php file
- $filename = $_server[' Request_uri '); The requested URL
- URL of the/** request, "/test3/test.php/user/check/tank"
- * test.php the PHP file to go to
- * User is class name
- * Check is the method name in class
- * Tank is the parameter to be passed to check
- * by bbs.it-home.org
- */
- Preg_match ("/(\w+\.php)/", $filename, $match); Find PHP file names
- $array = explode ('/', $filename); To split a static URL
- $key = Array_keys ($array, $match [0]); Get the subscript array corresponding to the file ([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]]); The package letter requests the PHP file in the URL, here is test.php
- }
- if (Class_exists ($param _array[0])) {//Determine test.php This file has the user class
- $obj = new $param _array[0];
- if (Method_exists ($obj, $param _array[1])) {//Determine if the user has check in this class
- $obj $param _array[1] ($param _array[3]); Call this method, the result is (my name is called tank)
- }
- }
- ?>
Copy CodeFive, test.php file
- Class User {
- Public function Check ($name) {
- echo "My name is called". $name;
- }
- }
- ?>
Copy CodeAt this point, when accessing Http://localhost/test3/test.php/User/check/tank, there will be no error. |