Mod_rewrite is a very powerful feature of Apache that can implement pseudo static pages. Let me elaborate on how it is used.
1. Detect if Apache supports Mod_rewrite
View the environment configuration through the Phpinfo () function provided by PHP, and find the "Loaded Modules" through the ctrl+f, which lists all the modules that Apache2handler have opened, and if it includes "mod_rewrite", it has been supported, You no longer need to continue setup.
If "Mod_rewrite" is not turned on, open the directory under your Apache installation directory "/apache/conf/" under the httpd.conf file, through ctrl+f find "LoadModule rewrite_module", the previous " # "number deleted."
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 Apache server support. htaccess
How do I get my local Apache server to support ". htaccess"? In fact, as long as a simple change to the Apache httpd.conf settings can let Apache support. htaccess. Open the httpd.conf file (where?) The Apache directory,
When opened with a text editor, find
Copy Code code as follows:
Options FollowSymLinks
AllowOverride None
To
Copy Code code as follows:
Options FollowSymLinks
AllowOverride All
It's OK.
3. Establishment of. htaccess Documents
There are 1 simplest ways to create. htaccess files:
Open with Notepad, click on file – Save As, enter ". htaccess" in the filename window, note that the whole green part,
Which is to include the English quotes, and then click Save.
4.rewrite Rule Learning
After we create a new. htaccess file, we write the following in it:
Rewriteengine on #rewriteengine为重写引擎开关on为开启off为关闭
Rewriterule ([0-9]{1,}) index.php?id=1
Let me explain. Rewriterule:rewriterule is an overriding rule that supports regular expressions, and the above ([0-9]{1,}) refers to the number of
The word is composed of, is the end sign, the description is to end with the number! OK, now that we can implement the pseudo static page, write down the rules: Rewriteengineonrewriterule ([A−za−z]1,) − ([0−9]1,). htmlindex.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, 1 represents the first
A bracket matches the value, 2 represents the second, so on!! We write a processing PHP script: index.phpphp code echo ' Your action is: ′._get[' action ';
Echo '
';
Echo ' Your ID is: '. $_get[' id '];
?>
Okay, now we're going to type in the browser:
Localhost/page-18.html
The output is:
Your action is: page
Your ID is: 18
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
To see that Apache httpd.conf does not have loadmodule rewrite_module modules/mod_rewrite.so this line, you need to build this module manually by following these measures:
The Apache source package path is:/ftp/httpd2.2
Apache Installation path:/usr/local/apache/
One, add mod_rewrite module
Copy Code code as follows:
# Find. -name mod_rewrite.c//Find mod_rewrite.c files in the Apache source installation directory
./modules/mappers/mod_rewrite.c
# Cd/ftp/httpd2.2/modules/mappers/Enter the directory containing MOD_REWRITE.C files
#/usr/local/apache/bin/apxs-c mod_rewrite.c//apxs should specify an absolute path in the bin directory where you are currently using Apache
#/usr/local/apache/bin/apxs-i-a-n mod_rewrite mod_rewrite.la
If there is nothing wrong, you should compile a mod_rewrite.so file in your Apache modules directory (/usr/local/apache/modules).
Edit the httpd.conf file to confirm that the httpd.conf already contains the mod_rewrite.so load statement, as follows:
LoadModule Mod_rewrite_module modules/mod_rewrite.so
Second, restart Apache
1. If the following error occurs:
Httpd:syntax error on line Of/usr/local/apache/conf/httpd.conf:can ' t locate API module structure ' Mod_rewrite_module ' In file/usr/local/apache/modules/mod_rewrite.so:/usr/local/apache/modules/mod_rewrite.so:undefined symbol:mod_ Rewrite_module
Need to put
LoadModule Mod_rewrite_module modules/mod_rewrite.so
To
LoadModule Rewrite_module modules/mod_rewrite.so
2. If the following error occurs
Httpd:syntax error on line Of/usr/local/apache/conf/httpd.conf:module Rewrite_module are built-in and can ' t be loaded
Indicates that the module is built inside, no need to be transferred, comment out
#LoadModule Rewrite_module modules/mod_rewrite.so
Opening URL rewriting for a virtual site
1, edit httpd-vhost.conf, add in the corresponding site tag:
Copy Code code as follows:
Directory/website/weba
AllowOverride All
/directory
That
Copy Code code as follows:
VirtualHost *:80
Documentroot/website/weba
ErrorLog Logs/weba.com-error_log
Customlog Logs/weba.com-access_log Common
Directory/website/weba
AllowOverride All
/directory
/virtualhost
Iv. Save the site rewrite rules to the site root directory with the file name ". htaccess"
Five, restart Apache can