Common applications and examples of Apache Rewrite Rules

Source: Internet
Author: User
Common applications and instance descriptions of Apache Rewrite Rules-Linux Enterprise applications-Linux server applications. For details, refer to the following section. 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, if a website has a regular URL and an alias URL, and the alias URL is redirected to a regular URL or the website is 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 runs on the company firewall to enable 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 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 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.
QUOTE:
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.