First, let’s talk about how to write the URL rewriting rules. This is mainly used for pseudo-static. Rewrite rules are placed under the .htaccess file or the Apache configuration file (some specific website configuration files in the httpd.conf or sites_conf directory). This article mainly discusses the .htacess file (actually they are all similar).
Open your .htacess file with the following content at the beginning:
RewriteEngine on
RewriteBase /
RewriteEngine on means turning on rewrite, and RewriteBase / means that all rules exist before "/ ", after writing this, you only need to write "^" at the beginning of all subsequent rules. However, it should be noted that this rule is only supported in the .htacess file. If it is a server configuration file, this rule It will be ignored and does not need to be written.
2
Before writing the rules, let me first tell you the meaning of the special characters designed in the rewriting.
* represents the previous 0 or more characters
+ represents the previous 1 or more characters
? Represents the first 0 or 1 characters
^ represents the starting position of the string
$ represents the end position of the string
. It is a wildcard character, representing any character
, which will restore the character that follows it to the character itself. For example, "+" represents "+" and not other meanings.
^ means not in square brackets. For example [^.] represents a non-wildcard character.
So if we want to pseudo-statically change URLs such as "http://www.AAA.com/b.php?id=1" to "http://www.AAA.com/b/1" form, the rules are as follows:
RewriteRule ^b/([0-9]+)/?$/b.php?id=$1 [L]
In the above example, ^ represents the subsequent domain name At the beginning of the URL, $ represents the end of the string, that is, the end of the URL after the domain name. [0-9] represents a number, because as long as the URL exists, there must be an id number. The + immediately following it indicates that 1 or more numbers appear. [0-9]+ are connected together, indicating that the characters appear in the form of numbers, the number range is 0-9, and the number of occurrences is 1 or multiple times. $ indicates the end of the string, ?$ indicates the end of the domain name after the root domain name, with 0 or 1 characters. $1 represents the content in memory, that is, the content in the first parentheses. [L] means stop if the rule matches, otherwise you need to continue running the remaining rules. Usually there is no need to go into this.
3
In addition to pseudo-static, sometimes we also need to use .htacess for 301 redirection. The meaning of the symbols remains unchanged, for example:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^AAA.com$ [NC]
RewriteRule ^(.*) http://www.AAA.com /$1 [R=301,L]
The above rule is to change AAA.com 301 to www.AAA.com. I believe the meaning of the symbols does not need to be explained.
So after these two rules, you should be inspired. What should you do if your website is revised and your URL structure changes? The answer is simple. We can use the .htacess file to 301 the old URL to the new URL by writing rewrite rules.
RewriteRule ^(.*?)/?index.html$/$1? [L,R=301]
This rule is to search the URL from a/index.html 301 to a/ under the domain name Down. The rules are dead. The most important thing is that everyone should learn and apply them. I believe that after mastering the rewrite rules, you will find that all the URLs and return codes of the website will be under your control!
The above is the writing method and rules after the apache server turns on rewrite. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!