The method of realizing pseudo-static in PHP

Source: Internet
Author: User
Tags php code php script

Mod_rewrite is a very powerful feature of Apache that can implement pseudo static pages. Let me elaborate on its use method
1. Detect if Apache supports Mod_rewrite
Phpinfo () function in PHP to view the environment configuration, through Ctrl+f find "Loaded Modules", It lists all modules that have been opened by Apache2handler, and if it includes "mod_rewrite", it is supported and no longer needs to be set.
If "mod_rewrite" is not turned on, open the httpd.conf file under your Apache installation directory "/apache/conf/" and find "LoadModule rewrite_module" through Ctrl+f. Remove the front "#" number.
If it is not found, go to the LoadModule area, add "LoadModule Rewrite_module, modules/mod_rewrite.so" (the required exclusive line) on the last line, and then restart the Apache server.
2. Let the Apache server support. htaccess
How do I get my local Apache server to support ". htaccess"? In fact, simply modify the Apache httpd.conf settings to make Apache Htaccess. Open the httpd.conf file (where?) The Conf directory of the Apache directory),
Find
Options followsymlinks
allowoverride None
When opened with a text editor to
Options FollowSymLinks
AllowOverride all
is OK.
3. Create. htaccess files
There are 1 easiest ways to create. htaccess files:
Open with Notepad, click on file – Save As, enter ". htaccess" in the filename window, note that the whole green section,
that contains the English quotes, Then click Save on the line.
4.rewrite Rule Learning
After we create a new. htaccess file, we write the following:
Rewriteengine on #rewriteengine为重写引擎开关on为开启off为关闭
Rewriterule ([0-9]{1,}) $index. php?id=$1
I'll explain. Rewriterule:rewriterule is an overriding rule that supports regular expressions, and the above ([0-9]{1,}) is a number
Word, and $ is the ending sign, which means ending with a number!
OK, now we can implement the pseudo static page, and write down the rules:
Rewriteengine on
Rewriterule ([A-za-z]{1,})-([0-9]{1,}). html$index.php? Action=$1&id=$2
([A-za-z]{1,})-([0-9]{1,}). html$ is the rule, index.php?action=$1&id=$2 is the format to be replaced, represents the
A bracket matches the value, and the $ $ represents the second one, so on!!
We write a processed PHP script:
index.php
PHP code
Echo ' Your action is: '. $_get[' action ';
Echo '
';
Echo ' Your ID is: '. $_get[' id '];

OK, we now enter in the browser:
localhost/page-18.html
Output is:
Your action is: page
your ID is:
 

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.