Common application of Apache rewriting rules and its illustration

Source: Internet
Author: User
Tags definition manual final relative domain domain name firewall

The purpose of this article is to provide an example of how to use the Apache rewrite rules to solve some common URL rewriting methods and to give users some basic methods and clues for using rewrite rules through common examples.

Why do I need to use rewrite rules

The life of the Web site is constantly updated and maintained, according to the needs of the business development of the transfer server to maintain, organize 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 virtual host 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 or a virtual host based on the configuration file, and then matches the rewrite rule based on 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

Ask the browser to issue 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 rewrite URL, the agent module to request content and return the final content to the customer, the client browser does not have to request again, the browser URL will not be overridden, but the actual content by the Apache based on the URL after the rewrite rules generated.

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 reaches the definition in the Apache,apache call rewrite rule to override the URL of the specified request by the user's browser, the last rewritten 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 permissions to the directory where the content of the Web site is located, 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

Suppose 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

(1) The realization method of httpd.conf

We put the following sections 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 rewrite section of the following Rewriterule definition (here is the filename index.html.en), it is a relative directory, relative to the definition behind this rewritebase. Usr/local/apache/htdocs/index.html.en, otherwise, if there is no "rewritebase/" here, it is rewritten as http://192.168.1.56/usr/local/apache/ Htdocs/manual/index.html.en, it is clearly not true.

Instead of "rewritebase/", we can change it to the following section.

       
        
         
        Rewriteengine on Rewriterule ^ (. *) $/index.html.en [r=301]
       
        

or change to:

       
        
         
        Rewriteengine on  rewriterule ^ (. *) $ http://192.168.1.56/index.html.en [r=301]
       
        

(2). The realization method of htaccess

We put the following sections in the httpd.conf.

       
        
         
        < Directory "/usr/local/apache/htdocs/manual/" > Options Indexes followsymlinks  allowoverride All </ Directory >
       
        

Then put 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 need 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]
       
        

In this way, requests for any file under the manual directory are redirected to the same file under the ~jephe directory.

2. Convert http://www.username.domain.com to Username's master request to Http://www.domain.com/username request

The request for http/1.1 includes a host:http header that we can rewrite with the following rule set

       
        
         
        Http://www.username.domain 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 a variety of variables, please consult the relevant 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  www.domain.com rewriteengine on Proxyrequest on Rewriterule ^/(. *) $ http://192.168.1.3/$1 [p,l] </virtualhost >
       
        

Note: When the external browser request http://www.domain.com, will be resolved to the IP address 1.2.3.4,apache to mod_rewrite processing, converted to http://192.168.1.3/$1 and then to the agent module Mod_ Proxy, after which the content is transferred back to the user's browser.

4. Basic Preset Transformation Map table for rewriting Rewritemap

Converts http://www.domain.com/{countrycode}/anypath to the URL specified in the map table, preceded by a definition in the virtual host.

       
        
         
        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]
       
        

The contents of document/usr/local/apache/conf/rewrite.map are as follows:

       
        
         
        SG http://a.b.c.d/sh http://e.f.g.h/
       
        

Note: When the user requests Http://www.domain.com/sg/anypath, it is rewritten as Http://a.b.c.d/anypath. 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.

Sitemap's syntax is ${sitemap:lookupkey | DefaultValue}, it is wrong to write $% in some books.



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.