Apache pseudo static Rewrite details

  • 2020-05-09 19:45:21
  • OfStack

1. Introduction to Rewrite rules:
The main function of Rewirte is to realize URL jump, and its regular expression is based on Perl language. This can be done both at the server level (httpd.conf) and at the directory level (.htaccess). If you want to use the rewrite module, you must first install or load the rewrite module. There are two methods: one is to directly install rewrite module when compiling apache, and the other is to install apache module in DSO mode when compiling apache, and then use the source code and apxs to install rewrite module.
2. Enable Rewrite in the Apache configuration
Open the configuration file httpd.conf:


1. To enable the rewrite
# LoadModule rewrite_module modules/mod_rewrite.so  Remove the front  #
2. To enable the .htaccess
 In the virtual machine configuration item 
AllowOverride None     Is amended as:  AllowOverride All

2. Rewrite
The server has configuration files that can't be changed by us, so most of the time you have to create a.htaccess file in the root of the site.

RewriteEngine on    // Start the rewrite engine 
RewriteRule ^/index([0-9]*).html$ /index.php?id=$1   // " ([0-9]*) "   On behalf of the range   with (.*) For all, same below. 
RewriteRule ^/index([0-9]*)/$ /index.php?id=$1 [R]   // Virtual directory 

3. Apache mod_rewrite rule rewrite flag 1 view
1) R[=code](force redirect) forces external redirection
Force http://thishost[:thisport]/ prefix to redirect to external URL. If code is not specified, the default 302 HTTP status code will be used.
2) F(force URL to be forbidden) disables URL and returns 403HTTP status code.
3) G(force URL to be gone) forces URL to be GONE, returning 410HTTP status code.
4) P(force proxy) forces the use of proxy forwarding.
5) L(last rule) indicates that the current rule is the last rule, and the rewriting of the rule after analysis is stopped.
6) N(next round) runs the rewrite process again from rule 1.
7) C(chained with next rule) is associated with the next rule
This flag is invalid if the rule matches, and if it does not, all the associated rules below are skipped.
8) T= MIME-type (force MIME type) enforces MIME type
9) NS (used only if no internal sub-request) is only used for non-internal sub-requests
10) NC(no case) is not case sensitive
11) QSA(query string append) append the request string
12) NE(no URI escaping of output) does not output escape special characters
For example: RewriteRule /foo/(.*) /bar? arg= P 1%3d$1 [R,NE] will correctly convert /foo/zoo to /bar? arg = P1 = zoo
13) PT(pass through to next handler) is passed to the next process
Such as:
RewriteRule ^/abc(.*) /def$1 [PT] #  Will be handed over to /def Rule processing 
Alias /def /ghi

14) S=num(skip next rule(s)) num rules
15) E=VAR:VAL(set environment variable) sets the environment variable

4. Examples of Apache rewrite
Example 1:
Meet the following two requirements at the same time:
In / / www. ofstack. com/xxx php to access / / www ofstack. com/xxx /
. 2. Use http: / / yyy ofstack. com to access / / www ofstack. com/user php? username = yyy function


RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.ofstack.com
RewriteCond %{REQUEST_URI} !^user.php$
RewriteCond %{REQUEST_URI} .php$
RewriteRule (.*).php$ //www.ofstack.com/$1/ [R]
RewriteCond %{HTTP_HOST} !^www.ofstack.com
RewriteRule ^(.+) %{HTTP_HOST} [C]
RewriteRule ^([^.]+).ofstack.com //www.ofstack.com/user.php?username=$1

Example 2:


/type.php?typeid=*  � > /type*.html
/type.php?typeid=*&page=*  � > /type*page*.html
RewriteRule ^/type([0-9]+).html$ /type.php?typeid=$1 [PT]
RewriteRule ^/type([0-9]+)page([0-9]+).html$ /type.php?typeid=$1&page=$2 [PT]


Related articles: