How to implement PHP URL pseudo-static

Source: Internet
Author: User
  1. Convert URLs to static URLs

  2. function Url_rewrite ($file, $params = Array (), $html = "", $rewrite = True) {
  3. if ($rewrite) {//development phase is not rewrite, where the development of the time, put $rewrite = False
  4. $url = ($file = = ' index ')? '' : '/' . $file;
  5. if (! empty ($params) && Is_array ($params)) {
  6. $url. = '/'. Implode ('/', Array_slice ($params, 0, 2));
  7. $param = Array_slice ($params, 2);
  8. foreach ($param as $key = = $value) {
  9. $url. = '/'. $key. '/' . UrlEncode ($value);
  10. }
  11. }
  12. if (! empty ($html)) {
  13. $url. = '. '. $html;
  14. }
  15. } else {
  16. $url = ($file = = ' index ')? '/' : '/' . $file;
  17. if (substr ($url,-4)! = '. php ' && $file! = ' index ') {
  18. $url. = '. php ';
  19. }
  20. if (! empty ($params) && Is_array ($params)) {
  21. $url. = '? '. Http_build_query ($params);
  22. }
  23. }
  24. return $url;
  25. }
  26. echo url_rewrite (' Test ', Array (' class ' + ' User ', ' act ' = ' check ', ' name ' = ' tank ', ' page ' =>5);
    ";
  27. $rewrite = False, the following/test.php?class=user&act=check&name=tank is displayed

  28. echo url_rewrite (' test.php ', Array (' class ' = ' + ' User ', ' act ' = ' check ', ' name ' = ' tank '));
    ";

  29. $rewrite = True, the following/test.php/user/check/tank are displayed
  30. echo url_rewrite (' Test ', Array (' class ' + ' User ', ' act ' = ' check ', ' name ' = ' tank '));
    ";
  31. $rewrite = True, the following/test/user/check/tank are displayed

  32. echo url_rewrite (' Test ', Array (' class ' = ' + ' User ', ' act ' = ' check ', ' name ' = ' tank '), ' HTML ');
    ";

  33. $rewrite = True, the following/test/user/check/tank.html are displayed
  34. ?>

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

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

Code 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

  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. * by bbs.it-home.org
  8. */
  9. Preg_match ("/(\w+\.php)/", $filename, $match); Find PHP file names
  10. $array = explode ('/', $filename); To split a static URL
  11. $key = Array_keys ($array, $match [0]); Get the subscript array corresponding to the file ([0] = 2)
  12. $file _array = array_slice ($array, 0, $key [0]+1); Array ([0] = [1] = test3 [2] = = test.php)
  13. $param _array = Array_slice ($array, $key [0]+1); Array ([0] = User [1] = check [2] = = tank)
  14. $file _path = implode ('/', $file _array);
  15. if ($array [$key [0]]! = "index.php") {
  16. Include_once ($array [$key [0]]); The package letter requests the PHP file in the URL, here is test.php
  17. }
  18. if (Class_exists ($param _array[0])) {//Determine test.php This file has the user class
  19. $obj = new $param _array[0];
  20. if (Method_exists ($obj, $param _array[1])) {//Determine if the user has check in this class
  21. $obj $param _array[1] ($param _array[3]); Call this method, the result is (my name is called tank)
  22. }
  23. }
  24. ?>
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

At this point, when accessing Http://localhost/test3/test.php/User/check/tank, there will be no error.

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