This article comes from: http://www.linuxforum.net of: Miss A Pavilion Jephe wu (2001-09-05 08:00:00)
This paper provides how to use Apache rewrite rules to address some common methods of URL rewriting problem to the user through a common example of some of the basic methods to use rewrite rules and clues.
2: Why do I need to use rewrite rules?
A website on the internet if long-term need to provide services, must be continually updated and maintained, such as temporary transfer to other server maintenance, re-organized directory structure, changing to a new URL or domain name changes, etc.
In order to allow customers will not be affected in any way, the best way is to use Apache Rewrite Rule (rewrite rules).
3: The role of the scope of rewrite rules
1) can be used in the main configuration file httpd.conf in Apache
2) You can use the definition in the httpd.conf in the virtual host configuration
3) You can use the cross in the base directory configuration file. Htaccess in the
4: Rewrite the rules of application conditions only when the user requests WEB eventually directed to a WEB server Apache Taiwan background, then this sets WEB server to accept incoming request, according to the request is the main configuration file configuration or virtual host, then under user requests in the browser
URI rewriting rules to match the request and the actual path to match. Htaccess rewrite rules in. Finally the contents of the request back to the user, the response may have two:
1) The browser requests the content of external redirection (Redirect) to another URL.
Let the browser again to the new URI request (R = 301 or R = 302, temporary or permanent redirect)
Such as: a website with regular URL and alias URL, URL of the Alias to redirect to a regular URL, or Web site put the change into the new domain redirect the old domain to new domain (Redirect)
2) may also be requested by the Apache internal sub-agents to generate new content back to the customer [P, L]
This is after the Apache URI rewriting internal according to internal content through a proxy module and return the contents of the request to the client, but does not know the client browser, the browser will not be rewritten URI. However, the actual content is Apache rewrite rules under the URI received.
Such as: the corporate firewall running Apache rewrite rules to begin such a proxy, the proxy on the internal network segment on the WEB server requests.
5: How to rewrite the rules work?
We assume that the time of compiling the Apache mod_rewrite module compiled, make sure your httpd.conf has
LoadModule rewrite_module libexec / mod_rewrite.so
And there in Addmodule
You can use the rewrite rules.
When the external request to Apache, Apache rewrite rules is called to rewrite the definition of specified by the user's browser requests
URI, final URI to be rewritten if redirected, then sent by the browser to make another request; If it is after the agency put the rewriting request URI to the proxy module final content (Content), the final return to contents to the browser.
6: When to Use. Htaccess rewrite the rules of the definition?
If you are content to your site where the server does not have administrator rights, or your website hosted on the ISP's server, so conditions, you can not rewrite the main configuration file, but you can on your WEB site content is located directory has write permissions, then you can set your own. htaccess
File to achieve the same purpose. But you need to identify the main configuration file on your website directory where the definition of the following:
Options Indexes FollowSymLinks
Otherwise your. Htaccess will not work.
7: The example assumes that Apache is compiled installed in the host 192.168.1.56 in / usr / local / apache / directory the following, we compiled a rewrite and proxy modules.
1) hide a directory under Apache, make any request to the directory redirects to another file.
a> httpd.conf Realization
We put some of the following to / usr / local / apache / conf / httpd.conf
options Indexes followsymlinks
rewriterule ^(.*)$ index.html.en [R = 301]
Note: rewriteengine on to rewrite the engine switch, if set off, then any definition of rewrite rules will not be applied, the switch has the added advantage that if the temporary removed to rewrite the rules changed to off and then restart Apache you can, do not have to rewrite the rules of Article 1 the following comment.
rewritebase /'s role is, if the definition in the following rewriterule be rewritten after some (here, the file name index.html.en) not preceded by /, is a relative directory, relative to the back of this rewritebase definition is / usr / local / apache / htdocs / index.html.en, or, if here is not rewritebase / this one, were re-written
http://192.168.1.56/usr/local/apache/htdocs/manual/index.html.en, is clearly not correct.
But here we can not rewritebase /, instead of
rewriterule ^(.*)$ / index.html.en [R = 301]
rewriterule ^(.*)$ http://192.168.1.56/index.html.en [R = 301]
b>. htaccess Realization
Let's put some of the following to httpd.conf
options Indexes followsymlinks
Then put the following section to / usr / local / apache / htdocs / manual / .htaccess in
rewriterule ^(.*)$ index.html.en [R = 301]
Note: the file. Htaccess does not need any changes made to restart Apache.
Q: If this manual directory redirects to the user's own home directory jephe it?
With the following. Htaccess program.
rewritebase / ~ jephe /
rewriterule ^(.*)$ $ 1 [R = 301]
Manual directory is on request of any document to be redirected to the same file ~ jephe directory requests.
2) conversion www.username.domain.com the request for the home page for the username
www.domain.com / username
For HTTP/1.1 requests include a Host: HTTP header, we can use the following set of rewriting rules
http://www.username.domain.com/anypath to / home / username / anypath
rewritecond% (HTTP_HOST) ^ www \ .[^.]+ \. host \. com $
rewriterule ^(.+)% (HTTP_HOST) $ 1 [C]
rewriterule ^ www \ .([^.]+) \. host \. com (.*) / home / $ 1 $ 2
rewritecond conditions of rewrite rules, when the conditions meet the definition of the back until after the application of the following rewrite rules
rewritecond a variety of variables, please refer to the documentation.
3) rewrite the rules on the firewall proxy server within the network segment on the request.
rewriterule ^/(.*)$ http://192.168.1.3/ $ 1 [P, L]
Note: When an external browser requests www.domain.com to the IP address is resolved when the 18.104.22.168, Apache surrender
mod_rewrite processing into
http://192.168.1.3/ $ 1 and then received by the proxy module mod_proxy content is sent back to the user's browser.
4) The basic pre-set conversion table rewriting rewritemap MAP
Conversion www.domain.com/ (countrycode) / anypath to the Map table provides the URI, the above is the definition of the virtual host
rewritelog / usr / local / apache / logs / rewrite.log
rewritemap sitemap txt: / usr / local / apache / conf / rewrite.map
rewriterule ^/([^/]+)+/(.*)$ http://% (REMOTE_HOST):: $ 1 [C]
rewriterule (.*)::([ az] +) $ $ (sitemap: $ 2 | http://hijk/) [R = 301, L]
File / usr / local / apache / conf / rewrite.map reads as follows:
Note: When a user requests to be rewritten when http://www.domain.com/sg/anypath
When you need to debug, please use the joint rewritelog and rewriteloglevel 9, 9 for the most get the most of debugging information that the minimum is 1, the minimum debugging information, the default is 0, no debugging information.
sitemap syntax is $ (sitemap: LookupKey | Defaultvalue), some books written to the% $ is wrong.