Common applications of Apache Rewrite Rules

Source: Internet
Author: User
Article Title: common applications of Apache rewrite rules. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Wu aiting
  
  
---- This article aims to provide some common URL rewriting methods by using Apache rewrite rules, and provide users with some basic methods and clues to use rewrite rules through common examples.
  
I. Why rewrite rules?
  
---- The website is constantly updated and maintained, it is often the case that the server is transferred for maintenance, directory structure re-organization, URL conversion, or even change to a new domain name based on the needs of business development. To prevent customers from being affected, the best way is to use Apache Rewrite Rule (Rewrite Rules ).
  
II. Scope of Rewrite Rules
  
---- 1. Use it in the Apache main configuration file httpd. conf.
---- 2. Use the virtual host configuration defined in httpd. conf.
---- 3. Use the. htaccess configuration file in the basic directory.
  
Iii. Application Conditions of Rewrite Rules
  
---- When your Web request is directed to the Apache daemon of a Web server, Apache determines whether the request is a master configuration or a virtual host based on the configuration file, then, the rewrite rule is matched based on the URL requested by the user in the browser and based on the actual Request Path. rewrite Rules in htaccess, and finally return the request content to the user. There may be two types of responses.
  
---- 1. Redirect the external request content (Redirect) to another URL
---- Let the browser send a request again with a new URL (R = 301 or R = 302, temporary or permanent redirection ).
  
---- For example, a website has a regular URL and an alias URL, Which is redirected to a regular URL or changed to a new domain name, the old domain name is redirected to the new domain name.
  
---- 2. The Apache internal sub-request proxy generates new content and sends it back to the customer [P, L]
---- This is based on the rewritten URL in Apache. The proxy module requests the content and sends the final content back to the client. The client browser does not have to make another request, and the URL in the browser will not be overwritten, however, the actual content is generated by Apache based on the URL after the rewrite rule.
  
---- For example, if Apache running on the company firewall starts this proxy rewrite rule, the proxy requests to the Web server on the Intranet segment.
  
4. How to rewrite rules
  
---- Assume that mod_rewrite has been compiled into a module during Apache compilation. Make sure that your httpd. conf contains LoadModule rewrite_module libexec/mod_rewrite.so and that Addmodule contains Addmodule mod_rewrite.c, you can use rewrite rules.
  
---- When an external request arrives at Apache, Apache calls the definition in the rewrite rule to rewrite the URL specified by the user's browser. If the URL to be overwritten is redirected, it is sent to the browser for another request; if it is a proxy, the rewritten URL is handed over to the proxy module to request the final Content, and finally the Content is sent back to the browser.
  
5. When to use the rewrite rule definition in. htaccess
  
---- If you do not have the Administrator permission on the server where the website content is located, or your website content is hosted on the ISP Server, you cannot rewrite the main configuration file, however, if you have the write permission on the directory where the Web site content is located, you can set your own. the htaccess file achieves the same purpose. However, you must make sure that the following content is defined in the directory where your website is located in the main configuration file, otherwise your. htaccess will not work.
  
---- <Directory/usr/local/apache/htdocs/www.abc.com> options indexes followsymLinks
---- Allowoverride all
---- </Directory>
  
Vi. Application Example
  
---- Assume that Apache is compiled and installed under the/usr/local/apache directory of host 192.168.1.56, and rewrite and proxy modules are compiled at the same time.
  
---- 1. Hide a directory under Apache so that any requests to this directory are redirected to another file.
---- (1) Implementation of httpd. conf
---- Put the following parts in/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 rewrite engine switch. If it is set to "off", any rewrite rule definition will not be applied, another use of this switch is to temporarily remove the rewrite rules, you can set the engine switch to "off" and then restart Apache, without commenting out all the rewrite rules.
  
---- "The rewritebase/example is the file name index.html. if "/" is not specified before, it indicates that it is a relative directory. The definition of this rewritebase is/usr/local/apache/htdocs/index.html. if no rewritebase/item exists, it is rewritten to http: // 192.168.1.56/usr/local/apache/htdocs/manual/index.html. obviously, it is incorrect.
  
---- We can also change it to the following part instead of "rewritebase.
---- Rewriteengine on
---- Rewriterule ^ (. *) $/index.html. en [R = 301]
---- Or change:
---- Rewriteengine on
---- Rewriterule ^ (. *) $ http: // 192.168.1.56/index.html. en [R = 301]
  
---- (2). Implementation of htaccess
---- Put the following part in httpd. conf.
  
---- <Directory "/usr/local/apache/htdocs/manual/"> options Indexes followsymlinks
---- Allowoverride all
---- </Directory>
  
---- Put the following parts in/usr/local/apache/htdocs/manual/. htaccess.
---- Rewriteengine on
---- Rewritebase/
---- Rewriterule ^ (. *) $ index.html. en [R = 301]
  
---- Note: you do not need to restart Apache for any changes made to file. htaccess.
  
---- You can also use the. htaccess solution to redirect this manual directory to your own jephe home directory.
---- Rewriteengine on
---- Rewritebase /~ Jephe/
---- Rewriterule ^ (. *) $1 [R = 301]
  
---- In this way, requests to any files in the manual directory are redirected ~ Request for the same file in the jephe directory.
  
---- 2. Convert the http://www.username.domain.com's home page request for username to a request for http://www.domain.com/username
---- For HTTP/1.1 requests include a Host: HTTP header, we can rewrite http://www.username.domain. com/anypath to/home/username/anypath with the following rule set.
---- Rewriteengine on
---- Rewritecond % {HTTP_HOST} ^ www. [^.] + .host.com $
---- Rewriterule ^ (. +) % {HTTP_HOST} $1 [C]
---- Rewriterule ^ www. ([^.] +) .host.com (. *)/home/$1 $2
  
---- Note: "rewritecond" indicates that it is a conditional rewrite rule. The following rewrite rules will be applied only when the conditions defined later are met. "rewritecond" has various variables. Please refer to the relevant documents.
  
---- 3. Rewrite Rules on the firewall proxy requests from servers on the Intranet segment
---- NameVirtualhost 1.2.3.4
---- <Virtualhost 1.2.3.4: 80> servername www.domain.com
---- Rewriteengine on
---- Proxyrequest on
---- Rewriterule ^/(. *) $ http: // 192.168.1.3/$1 [P, L]
---- </Virtualhost>
  
---- Note: when the external browser requests the http://www.domain.com, it will be resolved to the IP address 1.2.3.4, Apache by mod_rewrite processing, converted to http: // 192.168.1.3/$1 and then to the Agent module mod_proxy, the obtained content is sent back to the user's browser.
  
---- 4. Basically pre-defined conversion Map table to rewrite rewritemap
---- Convert http://www.domain.com/?countrycode=/anypathto the URL specified in the maptable, which is defined in the virtual environment.
---- Rewritelog/usr/local/apache/logs/rewrite. log
---- Rewriteloglevel 9
---- Rewriteengine on
---- Proxyrequest on
---- Rewritemap sitemap txt:/usr/local/apache/conf/rewrite. map
---- Rewriterule ^/([^/] +) +/(. *) $ http: // % {REMOTE_HOST}: $1 [C]
---- Rewriterule (. *): :( [a-z] +) $ {sitemap: $2 | http://h. I .j.k/} [R = 301, L]
---- File/usr/local/apache/conf/rewrite. map contains the following content:
---- Sg http://a. B .c.d/
-Sh http://e.f.g.h/
  
---- Note: when a user requests http://www.domain.com/sg/anypath, it is rewritten as http://a. B .c.d/anypath. When debugging is required, use rewritelog and rewriteloglevel 9. If 9 is the maximum, the most debugging information is obtained. If the minimum value is 1, the least debugging information is obtained. The default value is 0, indicates no debugging information.
  
---- The syntax of sitemap is $ {sitemap: LookupKey | Defaultvalue}. It is wrong to write $ as % in some books.
  
  
Related Article

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.