Pseudo-static of PHP redemption URL

Source: Internet
Author: User
Implementation of URL pseudo-static in PHP

The static URL, I generally with the help of apache,nginx,htaccess, and so on, seldom use PHP to achieve the static URL, with PHP to achieve static more trouble, so personally is not recommended to use PHP to implement URL static

One, the principle of static implementation of the URL

1, through the program to transform the dynamic URL into a static URL, the conversion method is best Unified interface. Put the static URL into the HTML so that we can see through the page is a static URL.

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

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

Second, convert the dynamic URL into a static URL

 $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 in case the following/test.php?class=user&act=check&name=tankecho url_rewrite (' test.php ', Array (' Class ' = "User", ' act ' = ' check ', ' name ' = ' tank '); echo "
";//$rewrite = True to show the following/test.php/user/check/tankecho url_rewrite (' Test ', Array (' class ' + ' User ', ' act ' = ') ' Check ', ' name ' = ' Tank '), echo "
";//$rewrite = True to show the following/test/user/check/tankecho url_rewrite (' Test ', Array (' class ' + ' User ', ' act ' = ' chec ') K ', ' name ' = ' tank '), ' HTML '); echo "
";//$rewrite = True to show the following/test/user/check/tank.html?>" User ", ' act ' = ' check ', ' name ' = ' tank '); > ">test

? Above simply write a method to convert the dynamic URL into a static URL, the page will generate a link as follows:

If you click here directly, you will be quoted 404 errors, because the root cannot find tank this directory. The difficulty is here too, so we have to specify a PHP file for directories and files that cannot be found. This should be used to Apache,nginx, or htaccess and so on.

Third, specify a unified portal

Rewritecond%{request_filename}!-f//cannot  find file Rewritecond%{request_filename}!-d  //cannot hit directory rewriterule./test3/ index.php [L]

?

Whether you do it in a. htaccess way, or in a configuration file such as Apache, it is possible. What does the above three sentences mean, if you can't find the directory to go to the index.php file, and if you can't find the file, go to index.php.

In doing so, when we visit the Http://localhost/test3/test.php/User/check/tank, we will go to index.php , since we know the PHP file, it is good to do.

The following things are done in the same way as Http://localhost/test3/test.php/User/check/tank, and the other way around.

Four, index.php file, URL mapping parsing class http://hudeyong926.iteye.com/admin/blogs/1113971

 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]]! = "IND            Ex.php ") {include_once ($array [$key [0]]); The package request PHP file in the URL, here is Test.php}if (class_exists ($param _array[0])) {//Determine test.php This file has user this 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)}}?>

? Five, test.php file

 
  

Here, when we visit the Http://localhost/test3/test.php/User/check/tank website,

The results are as follows: My name is tank, and the address bar 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.

  • 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.