Php URL pseudo-Static process detailed

Source: Internet
Author: User
  1. Convert URLs to static URLs
  2. function Url_rewrite ($file, $params = Array (), $html = "", $rewrite = True)
  3. {
  4. if ($rewrite) {//development phase is not rewrite, where the development of the time, put $rewrite = False
  5. $url = ($file = = ' index ')? '' : '/' . $file;
  6. if (!emptyempty ($params) && Is_array ($params)) $url. = '/'. Implode ('/', $params);
  7. if (!emptyempty ($html)) $url. = '. '. $html;
  8. } else {
  9. $url = ($file = = ' index ')? '/' : '/' . $file;
  10. if (substr ($url,-4)! = '. php ' && $file! = ' index ') $url. = '. php ';
  11. if (!emptyempty ($params) && Is_array ($params)) $url. = '? '. Http_build_query ($params);
  12. }
  13. return $url;
  14. }
  15. echo url_rewrite (' test.php ', Array (' class ' = "User", ' act ' = ' check ', ' name ' = ' tank '));
  16. $rewrite = False, the following/test.php?class=user&act=check&name=tank is displayed
  17. echo url_rewrite (' test.php ', Array (' class ' = "User", ' act ' = ' check ', ' name ' = ' tank '));
  18. $rewrite = True, the following/test.php/user/check/tank are displayed
  19. echo url_rewrite (' Test ', Array (' class ' = "User", ' act ' = ' check ', ' name ' = ' tank '));
  20. $rewrite = True, the following/test/user/check/tank are displayed
  21. echo url_rewrite (' Test ', Array (' class ' = "User", ' act ' = ' check ', ' name ' = ' tank '), ' HTML ');
  22. $rewrite = True, the following/test/user/check/tank.html are displayed
  23. ?>
  24. "User", ' act ' = ' check ', ' name ' = ' tank ')); > ">test
Copy Code

The above simply write a method to convert the dynamic URL into a static URL, the page will generate links as follows:

    1. Test
Copy Code

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

    1. Rewritecond%{request_filename}!-f//File not found
    2. Rewritecond%{request_filename}!-d//cannot hit directory
    3. Rewriterule. /test3/index.php [L]
Copy Code

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.

IV. index.php file

  1. $filename = $_server[' Request_uri '); The requested URL
  2. URL of the/** request, "/test3/test.php/user/check/tank"
  3. * test.php the PHP file to go to
  4. * User is class name
  5. * Check is the method name in class
  6. * Tank is the parameter to be passed to check */
  7. Preg_match ("/(\w+\.php)/", $filename, $match); Find PHP file names
  8. $array = explode ('/', $filename); To split a static URL
  9. $key = Array_keys ($array, $match [0]); Get the subscript array corresponding to the file ([0] = 2)
  10. $file _array = array_slice ($array, 0, $key [0]+1); Array ([0] = [1] = test3 [2] = = test.php)
  11. $param _array = Array_slice ($array, $key [0]+1); Array ([0] = User [1] = check [2] = = tank)
  12. $file _path = implode ('/', $file _array);
  13. if ($array [$key [0]]! = "index.php") {
  14. Include_once ($array [$key [0]]); The package letter requests the PHP file in the URL, here is test.php
  15. }
  16. if (Class_exists ($param _array[0])) {//Determine test.php This file has the user class
  17. $obj = new $param _array[0];
  18. if (Method_exists ($obj, $param _array[1])) {//Determine if the user has check in this class
  19. $obj $param _array[1] ($param _array[2]); Call this method, the result is (my name is called tank)
  20. }
  21. }
  22. ?>
Copy Code

Five, test.php file

    1. Class User {
    2. Public function Check ($name) {
    3. echo "My name is called". $name;
    4. }
    5. }
    6. ?>
Copy Code

Here, when we visit Http://localhost/test3/test.php/User/check/tank. The results are as follows: My name is tank, and the address bar remains static.

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