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
- <? PHP
- // Rewrite Test
- $ Uid = $ _ request ['today'];
- ?>
- <HTML>
- <Head>
- <Title> rewrite test </title>
- </Head>
- <Body bgcolor = "# ffffff" text = "#000000" link = "# 0000ff" vlink = "#800080">
- Today is <? PHP echo $ today;?>, What happened in history today? <Br>
- ......
- </Body>
- </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.
- <? PHP
- // URL example: soft. php/, 8630.html
- // 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.
- If (@ $ path_info = $ _ server ["path_info"]) {
- If (preg_match ("// (/d +), (/d +), (/d +)/. html/Si", $ path_info, $ arr_path )){
- $ Gid = intval ($ arr_path [1]); // get the value 1
- $ SID = intval ($ arr_path [2]); // get the value 100
- $ Softid = intval ($ arr_path [3]); // get the value 8630
- // Equivalent to soft. php? Gid = 1 & SID = 100 & softid = 8630
- } Else die ("Path: Error! ");
- } Else die ("Path: Nothing! ");
- Echo ($ GID );
- Echo ("<br> ");
- Echo ($ Sid );
- Echo ("<br> ");
- Echo ($ softid );
- ?>