Rewrite Study Notes

Source: Internet
Author: User
Tags url example

Submitted by on 2007, July 29.Website | PHP Learning

First of all, your space must support rewrite. I won't talk about the specific settings. I checked a lot on the search engine. Here is a simple introduction.

For example, below, I want to implement a URL like this:
Http://xxx.com/0725
Http://xxx.com/0726
Http://xxx.com/0727
...

The above xxx.com is a test site that records historical content such as "today is. The above link shows what happened in the history of July 25, similar to this. This looks beautiful and neat. Otherwise, the possible address is:
Http://xxx.com/index.php? T...
Http://xxx.com/index.php? T...
Http://xxx.com/index.php? T...
...

Now I want to implement index. php? Today = hide. The following code is used:

1. htaccess File
<Ifmodule mod_rewrite.c>
Rewriteengine on
Rewritebase/
Rewritecond % {request_filename }! -F
Rewritecond % {request_filename }! -D
Rewriterule ^ ([0-9] +) $/index. php? Today = $1
</Ifmodule>

In bold Chinese, I would like to explain that the other formats are in this format, but I do not understand it now.
[0-9] means that the parameter can only be 0 ~ 9. If you want to include any character, change it:

Rewriterule ^ (. +) $/index. php? Today = $1

Here [0-9] is changed to., which represents any character. Of course, the complexity is still complicated. We don't care about it for the time being.

2. index. php file

PHP code
  1. <? PHP
  2. // Rewrite Test
  3. $ Uid = $ _ request ['today'];
  4. ?>
  5. <HTML>
  6. <Head>
  7. <Title> rewrite test </title>
  8. </Head>
  9. <Body bgcolor = "# ffffff" text = "#000000" link = "# 0000ff" vlink = "#800080">
  10. Today is <? PHP echo $ today;?>, What happened in history today? <Br>
  11. ......
  12. </Body>
  13. </Html>

Here the parameter is passed to index. in this program, you can process the $ today in the PHP file based on parameters, such as querying the database and performing operations, and then display the corresponding data, you can.

 

 

 

  1. <? PHP
  2. // URL example: soft. php/, 8630.html
  3. // Use the server variable to obtain the path_info information. In this example, It is/, 8630.html, that is, the part after the execution Script Name.
  4. If (@ $ path_info = $ _ server ["path_info"]) {
  5. If (preg_match ("// (/d +), (/d +), (/d +)/. html/Si", $ path_info, $ arr_path )){
  6. $ Gid = intval ($ arr_path [1]); // get the value 1
  7. $ SID = intval ($ arr_path [2]); // get the value 100
  8. $ Softid = intval ($ arr_path [3]); // get the value 8630
  9. // Equivalent to soft. php? Gid = 1 & SID = 100 & softid = 8630
  10. } Else die ("Path: Error! ");
  11. } Else die ("Path: Nothing! ");
  12. Echo ($ GID );
  13. Echo ("<br> ");
  14. Echo ($ Sid );
  15. Echo ("<br> ");
  16. Echo ($ softid );
  17. ?>

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.