First, configure the server to start the rewrite module.
Open the configuration file httpd. conf of Apache.
Remove # before # LoadModule rewrite_module modules/mod_rewrite.
Restart Apache after saving
Write rewrite rules
For example, the code in test. php is
The code is as follows: |
Copy code |
<? Php $ Id = $ _ GET ["id"]; Echo $ id; ?> |
The function is to accept the id parameter in the URL and display it on the page.
First, use notepad (if not, use editplus) to create a document and save it as. htaccess. Write the following code in it:
The code is as follows: |
Copy code |
. <IFMODULE mod_rewrite.c> . RewriteEngine On . RewriteBase/ . RewriteRule ^ t _ (. * example .html $ test. php? Id = $1 [L] . </IFMODULE> |
For more information, see The apache manual. Download apache2.2 manual
Line 10 ^ t _ (. * pai.html $ describes the URL you entered, test. php? Id = $1 the actual address to access
For example, if you enter localhost/mytest/t_1.html in the browser (assuming that both your test. php and. htaccess files are in the mytest folder on your server), 1 is output in the browser,
If the input is localhost/mytest/t_sophp.html, the browser outputs sophp
The first address actually accesses localhost/mytest/test. php? Id = 1
The second actual access is localhost/mytest/test. php? Id = sophp
After the rule is rewritten, you can change the URL link of the previous parameter to the rule style you modified.
If the purchased space supports rewriting, you only need to upload the. htaccess file to the root directory.