Use of Apache ReWrite

Source: Internet
Author: User
Apache mod_rewrite is a killer module that provides powerful URL control. it can implement almost all URL control types you fantasized about. the price is that you must accept the complexity, because mod_rewrite is an important obstacle, it is difficult for beginners to understand and apply it easily, that is

Apache mod_rewrite is a killer module that provides powerful URL control. it can implement almost all URL control types you fantasized about. the price is that you must accept the complexity, because mod_rewrite is an important obstacle that beginners do not easily understand and apply, even Apache experts sometimes discover the new use of mod_rewrite.

In other words: mod_rewrite, or never use mod_rewrite, or love it for a lifetime.

ReWrite can be used in the following aspects or solve the following title:
URL plan
Standard URL
Clarification:
On some website servers, a resource may have multiple URLs. in actual use and announcement, Standard URLs should be applied. Others are abbreviated or internal applications. No matter what kind of URL the user applies in the request, what he sees should be a standard URL.

Plan:
Perform an external HTTP redirect for all nonstandard URLs to change its display in the browser address bar and its subsequent request. In the following example, the rule set is replaced by the standard/u/user /~ And corrected the slash of the suffix missing by/u/user.

Code:RewriteRule ^ /~ ([^/])/? (. *)/U/$1/$2 [R]
RewriteRule ^/([uge])/([^/]) $/$1/$2/[R]

Standard host name
Clarification:
...
Plan:
Code:RewriteCond % {HTTP_HOST }! ^ Fully \. qualified \. domain \. name [NC]
RewriteCond % {HTTP_HOST }! ^ $
RewriteCond % {SERVER_PORT }! ^ 80 $
RewriteRule ^/(. *) http://fully.qualified.domain.name: % {SERVER_PORT}/$1 [L, R]
RewriteCond % {HTTP_HOST }! ^ Fully \. qualified \. domain \. name [NC]
RewriteCond % {HTTP_HOST }! ^ $
RewriteRule ^/(. *) http://fully.qualified.domain.name/#1 [L, R]

Moved DocumentRoot
Clarification:
Generally, the DocumentRoot of the website server directly corresponds to the URL '/'. However, it is often not at the highest level, but may only be an entity in many data pools. For example, on an Intranet site, there are/e/www/(WWW homepage),/e/sww/(Intranet homepage), and so on, while DocumentRoot points to/e/www /, all embedded images and other elements in the data pool must be valid for subsequent requests.

Plan:
You only need to redirect the URL/to/e/www. This plan looks very simple, but only with the support of the mod_rewrite module, it is simple, because the traditional URL Aliases mechanism (provided by mod_alias and its coherent module) is only a prefix match, documentRoot is a prefix for all URLs, so such redirection cannot be implemented. The use of mod_rewrite is indeed very simple:

Code:RewriteEngine on
RewriteRule ^/$/e/www/[R]

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.