URL rewriting rules. This is mainly used for pseudo-static. Rewrite Rules are placed in. in the htaccess file or Apache configuration file (httpd. conf or some specific website configuration files in the sites_conf directory). This article mainly discusses. htacess files (in fact, almost all ).
Open your. htacess file with the following content starting:
Rewriteengine on
Rewritebase/
Rewriteengine on indicates that rewrite is enabled, and rewritebase/indicates that "/" exists before all rules. After this rule is written, the beginning of all rules after you start, you only need to write "^", but note that this rule is only in. the htacess file is supported. If it is a server configuration file, this rule is ignored and does not have to be written. Before writing rules, let's talk about the meaning of the special characters designed in rewriting.
* Represents the first 0 or more characters
+ Represents the first 1 or more characters
? Represents the first 0 or 1 Character
^ Represents the start position of the string
$ Indicates the position at which the string ends.
. Is a wildcard, representing any character
\ Restores the character following it to the character itself. For example, "\ +" indicates "+", rather than other meanings.
^ It indicates non-meaning in square brackets. For example, [^.] indicates a non-wildcard.
So if we want to change "http: // www. ***. com/B. php? The URL format of ID = 1 "is" http: // www. ***. com/B/1 ". The rules are as follows:
Rewriterule ^ B/([0-9] + )/? $/B. php? Id = $1 [l]
In the above example, ^ indicates the start of the URL of the domain name, $ indicates the end of the string, that is, the end of the URL after the domain name. [0-9] indicates a number, because as long as the URL exists, it must have an ID number. + Next to it indicates that one or more numbers appear. [0-9] +, indicating that the character appears as a number. The value range is 0-9, and the number of occurrences is one or more times. $ Indicates that the string ends ,? $ Indicates that the root domain name ends with the end of the domain name, with 0 or 1 character. $1 indicates that the content in memory exists, that is, the content in the first parentheses. [L] indicates that the rule is stopped if it matches. Otherwise, you need to continue to run the remaining rule. Generally, this does not need to be further investigated.
Unless it is pseudo-static, we sometimes need to use. htacess for 301 redirection. The meaning of the symbol remains unchanged, for example:
Rewriteengine on
Rewritecond % {http_host} ^ seozhao \. com $ [Nc]
Rewriterule ^ (. *) http: // www. ***. com/$1 [R = 301, l]
The above rules refer to seozhao.com 301 to www. ***. com. The meaning of the symbol is believed to be unnecessary.
After these two rules, you should be inspired. If your website is revised, your url structure will change. What should I do? The answer is simple. We can use the. htacess file to write a rewrite rule to 301 the old URL to the new URL.
Rewriterule ^ (.*?) /? Index \. html $/$1? [L, r = 301]
This rule is to search for a/index.html 301 to A/URL under the domain name. The rules are dead. The most important thing is that everyone needs to learn and use them. I believe that after mastering the rewrite rules, you will find that all the URLs and return codes of the website are under your control!