Two methods to implement pseudo-static in php

Source: Internet
Author: User
Two methods to implement pseudo-static in php

  1. /**
  2. * Php pseudo-static
  3. * Bbs.it-home.org
  4. */
  5. $ Conn = mysql_connect ("localhost", "root", "root") or dir ("connection failed ");
  6. Mysql_select_db ("tb_demo", $ conn );
  7. $ SQL = "select * from news ";
  8. $ Res = mysql_query ($ SQL );
  9. Header ("content-type: text/html; charset = utf-8 ");
  10. Echo "news list ";
  11. Echo "add news ";
  12. Echo"
  13. Echo"
  14. While ($ row = mysql_fetch_assoc ($ res )){
  15. }
  16. Echo"
  17. // The Red address above should have been show_news.php? Act = look & id = {$ row ['id']}
  18. Echo"
  19. "; "; ";
    Id Title View details Modify News
    {$ Row ['id']} {$ Row ['title']} View details Modify page
    ";
  20. // Close the resource
  21. Mysql_free_result ($ res );
  22. Mysql_close ($ conn );
  23. ?>

2. show_new.php page

  1. Header ("Content-type: text/html; charset = utf-8 ");
  2. $ Conn = mysql_connect ("localhost", "root", "root ");
  3. Mysql_select_db ("tb_demo", $ conn );
  4. Mysql_query ("set names utf8 ");
  5. $ Pa = $ _ SERVER ['path _ info'];
  6. // $ The value printed out by pa is/look-id-1.html
  7. // Use a regular expression to match the obtained url
  8. If (preg_match ('/^ \/(look)-(id)-([\ d]) \. shtml $/', $ pa, $ arr )){
  9. $ Act = $ arr [1]; // This is the look method of the request.
  10. $ Id = $ arr [3]; // This is the obtained id value.
  11. $ SQL = "select * from news where id = $ id ";
  12. $ Res = mysql_query ($ SQL );
  13. $ Res = mysql_fetch_assoc ($ res );
  14. Echo $ res ['title']. "". $ res ['content'];
  15. } Else {
  16. Echo "invalid url ";
  17. }
  18. Mysql_close ($ conn );
  19. ?>

2. according to the configuration. htaccess implementation first. create an htaccess file, create a notepad under the root directory of the website, and double-click it to open and click save as file name. htaccess: Select All files for the storage type, and select UTF-8 for encoding. you can see this in the directory. htaccess file.

First, enable mod_rewrite.so in apache, and replace AllowOverride None with AllowOverride All.

For example, the href address is written as a one_new-id-1.shtml // which means one_new.php? Id = 1 here. htaccess is written as follows:

  1. # Write your rewrite rules
  2. RewriteEngine On
  3. # You can configure multiple rules. The matching sequence is from top to bottom.
  4. RewriteRule one_new-id-(\ d +) \. shtml $ one_new.php? Id = $1 // Here $1 represents the first parameter.
  5. RewriteRule abc_id (\ d +) \. html $ error. php
  6. # Setting 404 errors
  7. # ErrorDocument 404/error. php

On the one_new.php page, echo $ _ GET ['id'] will certainly output the id value.

Let's talk about this. it is very easy to implement pseudo-static in php. I hope you can practice more and write more flexible and lightweight pseudo-static rules.

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.