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: