Common use of Apache rewrite rules and instance clarification

Source: Internet
Author: User
This article aims to provide users with basic methods and clues on how to use Apache rewrite rules to solve the title of some common URL Rewrite methods.

This article aims to provide you with basic methods and clues on how to use Apache rewrite rules to solve the titles of common URL Rewrite methods.

I. Why rewrite rules?

The website is constantly updated and protected, based on the needs of business development, it is often possible to transfer the server to protect, reorganize the directory structure, transform the URL, or even change to a new domain name. To prevent customers from being affected, the best way is to apply Apache Rewrite Rule (Rewrite rules ).

II. Scope of rewrite rules

1. the application is in the Apache Main configuration file httpd. conf.

2. apply the VM configuration defined in httpd. conf.

3. the application is in the. htaccess configuration file of the basic directory.

III. conditions for using rewrite rules

When a user's Web request is directed to the Apache daemon process of a Web server, Apache determines whether the request is a master configuration or a virtual host based on the configuration file, then, rewrite the rule based on the URL requested by the user in the browser, and match according to the actual request path. rewrite Rules in htaccess, and finally return the content of the request to the user. There may be two types of responses.

1. Redirect the external request content (Redirect) to another URL

Make the browser send a request again with a new URL (R = 301 or R = 302, temporary or permanent redirection ).

For example, if a website has a regular URL and a special URL, and the website 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. Apache requests the proxy to generate new content and send it back to the customer [P, L]

This is an internal rewrite URL of Apache. the proxy module requests the content and sends the final content back to the client. the client browser does not have to beg again, and the URL in the browser will not be overwritten, however, the actual content is generated by the URL after Apache rewrite rules.

For example, if Apache running on the corporate firewall starts this proxy rewrite rule, the proxy requests 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 apply rewrite rules.

When the external request reaches Apache, Apache calls the definition in the rewrite rules to rewrite the URL specified by the user's browser. if the URL to be rewritten is redirection, 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 ultimate Content, and finally send the Content back to the browser.

5. when to apply the rewrite rule definition in. htaccess

 

If you do not have administrator permissions 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 goal. However, you need to determine 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. Examples

It is assumed that Apache is compiled and installed under the/usr/local/apache Directory of host 192.168.1.56, and the rewrite and proxy modules are compiled at the same time.

1. hide a directory in Apache to redirect any requests to this directory 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 used, another function of this switch is to set the engine switch to "off" and restart Apache to temporarily rewrite the rules. it is not necessary to comment out the rewrite rules.

"The rewritebase/example is a token that is rewritten in the following rewriteruledefinition (this 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. it is obviously inaccurate.

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

We will put the following parts in httpd. conf.

< Directory '/usr/local/apache/htdocs/manual/'> options Indexes followsymlinks  allowoverride all < /Directory >
 

Then, 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 modifications made to file. htaccess.

You can also use the. htaccess plan to redirect this manual directory to your own jephe home directory.

rewriteengine on  rewritebase /~jephe/ rewriterule ^(.*)$ $1 [R=301]

In this way, the request for any files in the manual directory is redirected ~ Request for identical files under the jephe directory.

2. convert the http://www.username.domain.com's requests for the username home page to requests for http://www.domain.com/username

The request for HTTP/1.1 contains a Host: HTTP header, which can be rewritten using 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 that it is a condition rewriting rule. the following rewrite rule is used only when the conditions defined later are met. "rewritecond" has various variables. please refer to 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, 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. rewrite the rewritemap using the pre-defined conversion Map table.

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]

The 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, no debugging information is displayed.

The syntax of sitemap is $ {sitemap: LookupKey | Defaultvalue}. it is wrong to write $ AS % 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.