Common application of Apache rewrite rewrite rules _ server

Source: Internet
Author: User

Why do I need to use rewrite rules
----The life of the site is constantly updated and maintained, according to the needs of the business development of the transfer server maintenance, the organization of the directory structure, change the URL or even changed to the new domain name and so on often happen. In order for the customer to not be affected by this, the best approach is to use the Apache Rewrite rule (rewrite rules).
Ii. scope of the rewrite rules
----1. Used in the Apache main configuration file httpd.conf.
----2. Use the configuration defined in httpd.conf.
----3. Use the spanning configuration file in the base directory. htaccess.
III. application conditions of rewrite rules
----When a user's Web request is eventually directed to the Apache daemon for a Web server, Apache determines whether the request is a master configuration or, based on the configuration file, and then matches the rewrite rule according to the URL requested by the user in the browser. And according to the actual request path match. Rewrite rules in htaccess, and finally pass the requested content back to the user. There may be 2 of these responses.
----1. External redirection of Request content (Redirect) to another URL
----Let the browser make a request again with a new URL (r=301 or r=302, temporary or permanent redirection).
----For example, a site has a regular URL and alias URL, the alias URL to redirect to the normal URL, or the site changed to a new domain name, the old domain name redirect to the new domain name.
----2. Generate new content from Apache internal Child request agent send back to Customer [p,l]
----This is Apache internal based on the rewritten URL, request content through the proxy module and return the final content to the customer, the client browser does not have to request again, the URL in the browser will not be overridden, but the actual content is generated by Apache based on the URL after the rewrite rule.
----For example, Apache, which runs on a corporate firewall, initiates this proxy rewrite rule, which proxies requests for Web servers on the internal network segment.
Iv. how the Rewrite rules work
----We assume that when we compile Apache we have compiled mod_rewrite into modules, and we are sure that you have loadmodule rewrite_module in your httpd.conf libexec/mod_ Rewrite.so, and you have addmodule mod_rewrite.c in addmodule, you can use overriding rules.
----When an external request arrives at the definition in the Apache,apache call rewrite rule to override the URL specified by the user's browser, the last overridden URL, if it is redirected, is sent to the browser to make the request again; If the agent sends the rewritten URL to the agent module to request the final content ( Content), and finally send the contents back to the browser.
When to use. overriding rule definitions in htaccess
----If you do not have administrator rights on the server on which the site content resides, or if your site content is hosted on the ISP's server, you cannot overwrite the primary profile, but you have write permission to the directory where the Web site content resides, you can set your own. htaccess file to achieve the same purpose. But you need to make sure that the following content is defined in the main configuration file for your site's directory, otherwise your. Htaccess will not work.
----< directory/usr/local/apache/htdocs/www.abc.com> options Indexes FollowSymLinks
----allowoverride All
----</directory >
Vi. Examples of applications
----Assume that Apache is compiled and installed under the/usr/local/apache directory of the host 192.168.1.56, while the rewrite and proxy modules are compiled.
----1. Hides a directory under Apache so that any requests to that directory are redirected to another file
The realization method of----(1) httpd.conf
----We put the following part in the/usr/local/apache/conf/httpd.conf.
----< Directory "/usr/local/apache/htdocs/manual/" > Options Indexes followsymlinks
----allowoverride All
----Rewriteengine on
----Rewritebase/
----Rewriterule ^ (. *) $ index.html.en [r=301]
----</directory >
----Note: "Rewriteengine on" is the override engine switch, and if set to "off", any rewrite rule definition will not be applied, and another use of the switch is to set the engine switch to "off" and then restart Apache to temporarily remove the rewrite rule. You do not have to comment out each of these rewrite rules.
The effect of----"rewritebase/" is that if there is no "/" in front of the overridden section of the following Rewriterule definition (here is the filename index.html.en), then the relative directory, The definition behind this rewritebase is/usr/local/apache/htdocs/index.html.en, otherwise, if the term "rewritebase/" is not here, it is clearly incorrect.
----We can change it to the following section instead of "Rewritebase/".
----Rewriteengine on
----Rewriterule ^ (. *) $/index.html.en [r=301]
----or change to:
----Rewriteengine on
----Rewriterule ^ (. *) $ [r=301]
----(2). Realization method of Htaccess
----We put the following part in the httpd.conf.
----< Directory "/usr/local/apache/htdocs/manual/" > Options Indexes followsymlinks
----allowoverride All
----</directory >
----then place the following sections in the/usr/local/apache/htdocs/manual/.htaccess.
----Rewriteengine on
----Rewritebase/
----Rewriterule ^ (. *) $ index.html.en [r=301]
----NOTE: Any changes made to the file. htaccess do not require you to restart Apache.
----You can also use the. htaccess scheme to redirect this manual directory to the user Jephe own home directory.
----Rewriteengine on
----Rewritebase/~jephe/
----Rewriterule ^ (. *) $ $ [r=301]
----This way, requests for any file under the manual directory are redirected to the same file under the ~jephe directory.
----2. To convert a request to a username for a home page request
----The request for http/1.1 includes a host:http header, we can rewrite it with the following rule set. Com/anypath to/home/username/anypath.
----Rewriteengine on
----Rewritecond%{http_host} ^www. [^.] +.host.com$
----Rewriterule ^ (. +)%{http_host}$1 [C]
----Rewriterule ^www. ([^.] +). host.com (. *)/home/$1$2
----Note: "Rewritecond" indicates a conditional rewrite rule that applies the following rewrite rules when the conditions defined later are met, and "Rewritecond" has various variables, consult the documentation.
----3. Rewrite rules on the firewall request from the server on the internal network segment
----Namevirtualhost 1.2.3.4
----< VirtualHost 1.2.3.4:80 > ServerName
----Rewriteengine on
----Proxyrequest on
----Rewriterule ^/(. *) $ [p,l]
----</virtualhost >
----NOTE: When the external browser request, will be resolved to the IP address 1.2.3.4,apache to mod_rewrite processing, converted to the agent module Mod_proxy, after the contents of the transfer back to the user's browser.
----4. Basic Preset Transformation Map table for rewriting Rewritemap
The----is converted to the specified URL in the map table, preceded by the definition in.
----Rewritelog/usr/local/apache/logs/rewrite.log
----Rewriteloglevel 9
----Rewriteengine on
----Proxyrequest on
----Rewritemap Sitemap Txt:/usr/local/apache/conf/rewrite.map
----Rewriterule ^/([^/]+) +/(. *) $ [C]
----Rewriterule (. *)::([a-z]+) $ ${sitemap:$2|http://h.i.j.k/} [r=301,l]
The contents of the----file/usr/local/apache/conf/rewrite.map are as follows:
----SG
----SH
----NOTE: When a user requests it, it is rewritten as. When you need to debug with Rewritelog and Rewriteloglevel 9 Union, 9 is the largest, that is, get the most debugging information, minimum 1, to obtain the least debugging information; The default is 0, which means there is no debug information.
The syntax for----sitemap is ${sitemap:lookupkey | DefaultValue}, it is wrong to write $% in some books.
(Author: Wu Ating)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.