How PHP implements URL pseudo-static (goto)

Source: Internet
Author: User
Tags phpinfo

Apache Mod_rewrite is relatively strong, in the construction of the site, you can use this module to achieve pseudo-static.
The main steps are as follows:

1, detect whether Apache open Mod_rewrite function can be provided by the PHP phpinfo () function to view the environment configuration, find "Loaded Modules",

It lists all the modules that the Apache2handler has already opened, and if it includes "mod_rewrite", it is already supported and no longer required to continue setting.

If "Mod_rewrite" is not turned on, open the directory Apache directory "/apache/conf/", find the httpd.conf file, and then find "LoadModule rewrite_module",

Removing the previous "#" number means fetching the feature. If you do not find the "LoadModule" area, you can add "LoadModule rewrite_module, modules/mod_rewrite.so" (exclusive row) on the last line,

Then restart the Apache server. The Phpinfo () function then looks at the environment configuration with "Mod_rewrite" as the item.
2. Let Apache server support. htaccess How to make your native Apache server support: "Htaccess"? Just modify Apache's httpd.conf settings to allow Apache to support ". htaccess".

Open the httpd.conf file in the Conf directory of the Apache directory to find:

Options followsymlinks allowoverride None change to Options FollowSymLinks allowoverride all on the line.
3. Build the. htaccess file when you build the. htaccess file, be aware that it cannot be built directly by using the Save As menu in Notepad, entering the file name window: ". htaccess" and then clicking Save.
4, rewrite rule learning after you create a new. htaccess file, write the following in it: Rewriteengine on #rewriteengine为重写引擎开关on为开启off为关闭

Rewriterule ([0-9]{1,}) $index. php?id=$1 here, Rewriterule is the rewrite rule, which is a sentence with a regular expression, ([0-9]{1,}) that is made up of numbers,

$ represents the end flag, which means ending with a number! If you want to implement pseudo-static pages, the rules are as follows:

Rewriteengine on Rewriterule ([A-za-z]{1,})-([0-9]{1,}). html$ index.php?action=$1&id=$2

In a regular expression, ([A-za-z]{1,})-([0-9]{1,}). html$ is the rule, the index.php?action=$1&id=$2 is the format to be replaced, the value that represents the 1th bracket match, and the value of the second parenthesis.

So analogy! Test the PHP script as follows: The code in the index.php file is as follows: Echo ' Your Action value is: '. $_get[' action ']; Echo '; The echo ' ID value is: '. $_get[' id '];?>
In the browser address bar input: localhost/page-18.html output is: Your Action value is: Page ID value is: 18

ErrorDocument 404 error.html----can write error steering rules.
Rewriterule ([A-za-z]{1,})-([0-9]{1,}). html$ test.php?action=$1&id=$2

The above two rules were tested successfully in the test machine. Xxx.html (not present) This turns to execution error.html

Xxx-12.html This turns to test.php execution.

How PHP implements URL pseudo-static (goto)

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.