Application of Apache ReWrite

Source: Internet
Author: User
Apache mod_rewrite is a killer module that provides powerful URL operations. it can implement almost all URL operation types you dream of. The price is that you must accept the complexity, because the main obstacle of mod_rewrite is that it is not easy for beginners to understand and use it. even Apache experts sometimes discover the new use of mod_rewrite. In other words: for mod_rewrite, ApacheRewrite

Apache mod_rewrite is a killer module that provides powerful URL operations. it can implement almost all URL operation types you dream of. The price is that you must accept the complexity, because the main obstacle of mod_rewrite is that it is not easy for beginners to understand and use it. even Apache experts sometimes discover the new use of mod_rewrite.

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

ReWrite can be applied in the following aspects or solve the following problems:
URL planning
Standard URL
Note:
On some website servers, a resource may have multiple URLs. in actual application and release, standard URLs should be used, while others may be abbreviated or used internally. No matter what form of URL the user uses in the request, what he sees is a standard URL.

Solution:
Execute an external HTTP redirection for all nonstandard URLs to change the display in the browser address bar and its subsequent requests. In the following example, replace the rule set with 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
Note:
...
Solution:

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
Note:
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.

Solution:
You only need to redirect the URL/to/e/www. This solution looks simple, but it is only simple with the support of the mod_rewrite module, because the traditional URL Aliases mechanism (provided by mod_alias and its related modules) only makes a prefix match, documentRoot is a prefix for all URLs, so such redirection cannot be implemented. Using mod_rewrite is really simple:

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



Question about the suffix slash
Note:
Each network administrator has a bitter experience on the problem of referencing the directory suffix slash. if it is omitted, the server will generate an error because if the request is /~ Quux/foo instead /~ Quux/foo/, the server will find a file named foo, which is a directory, so an error is reported. In fact, in most cases, it will try to correct this error, but sometimes you need to manually correct it, for example, after rewriting complicated URLs in many CGI scripts.

Solution:
The solution to this subtle problem is to allow the server to automatically add a slash with the suffix. In this case, an external redirection must be used so that the browser can correctly process subsequent requests to the slices. If you only make an internal rewrite, it may only be valid for the directory page, but the page with an image embedded with a relative URL is invalid because the browser may request an embedded target. For example, if you do not need external redirection ,/~ For the Image.gif request in the quux/foo/index.html page, the result will be /~ Quux/image.gif !.

Therefore, you should write as follows:

Code:
RewriteEngine on
RewriteBase /~ Quux/
RewriteRule ^ foo $ foo/[R]


The lazy and crazy way is to write these into top-level. htaccess in the home directory, but note that this will bring some processing overhead.

Code:
RewriteEngine on
RewriteBase /~ Quux/
RewriteCond % {REQUEST_FILENAME}-d
RewriteRule ^ (. [^/]) $1/[R]



Similar URL planning for cluster websites
Note:
We hope to establish a consistent URL plan for all WWW servers in an Intranet cluster website, that is, all URLs (for a single server, is locally dependent on this server !) Is independent from the server! What we need is a WWW namespace with server-independent consistency plan, that is, the URL does not need to contain the correct physical target server, the cluster itself automatically locates the physical target host.

Solution:
First, the information of the target server comes from (generated) External maps containing users, groups, and entities. the format is as follows:

Code:
User1 server_of_user1
User2 server_of_user2
::



This information is stored in the map. xxx-to-host file. Second, if the URL is invalid on a server, you need to guide all servers to redirect URLs.

Code:
/U/user/anypath
/G/group/anypath
/E/entity/anypath


To

Code:
Http: // physical-host/u/user/anypath
Http: // physical-host/g/group/anypath
Http: // physical-host/e/entity/anypath



The following rule set relies on the map file to complete this operation (assuming that a user does not have a corresponding item in the map, server0 is used as the default server ):

Code:
RewriteEngine on

RewriteMap user-to-host txt:/path/to/map. user-to-host
RewriteMap group-to-host txt:/path/to/map. group-to-host
RewriteMap entity-to-host txt:/path/to/map. entity-to-host

RewriteRule ^/u/([^/])/? (. *) Http: // $ {user-to-host: $1 | server0}/u/$1/$2
RewriteRule ^/g/([^/])/? (. *) Http: // $ {group-to-host: $1 | server0}/g/$1/$2
RewriteRule ^/e/([^/])/? (. *) Http: // $ {entity-to-host: $1 | server0}/e/$1/$2

RewriteRule ^/([uge])/([^/])/? $/$1/$2/. www/
RewriteRule ^/([uge])/([^/])/([^.].)/$1/$2/. www/$3 \



Move the home directory to different website servers
Note:
Generally, when many network administrators create a new website server, they need to redirect all the home directories on one website server to another.

Solution:
Very simple. use mod_rewrite. Redirect all URLs on the old website server /~ User/anypath to http: // newserver /~ User/anypath.

Code:
RewriteEngine on
RewriteRule ^ /~ (.) Http: // newserver /~ $1 [R, L]



Structured home directory
Note:
Websites with thousands of users usually use structured home directory planning, that is, each home directory is located in a subdirectory with a specific prefix such as the first character of its user name. So ,/~ Foo/anypath indicates/home/f/foo/. www/anypath, while /~ Bar/anypath indicates/home/B/bar/. www/anypath.

Solution:
You can use the following rule sets to scale out ~ To achieve the above purpose.

Code:
RewriteEngine on
RewriteRule ^ /~ ([A-z]) [a-z0-9]) (. *)/home/$2/$1/. www $3



File system restructuring
Note:
This is not an example: a killer application that uses a large number of directory-specific rule sets to achieve a smooth perception, but never adjusts the data structure. Background: net. sw has stored free and valid Unix software packages I collected since 1992. It is my hobby and my job, because I have been a system and network administrator for many years while studying computer science. I had to sort out software every week, so I established a hierarchical directory structure to store various software packages:

Code:
Drwxrwxr-x 2 netsw users 512 Aug 3 Audio/
Drwxrwxr-x 2 netsw users 512 Jul 9 Benchmark/
Drwxrwxr-x 12 netsw users 512 Jul 9 Crypto/
Drwxrwxr-x 5 netsw users 512 Jul 9 Database/
Drwxrwxr-x 4 netsw users 512 Jul 30 Dicts/
Drwxrwxr-x 10 netsw users 512 Jul 9 Graphic/
Drwxrwxr-x 5 netsw users 512 Jul 9 0hackers/
Drwxrwxr-x 8 netsw users 512 Jul 9 InfoSys/
Drwxrwxr-x 3 netsw users 512 Jul 9 Math/
Drwxrwxr-x 3 netsw users 512 Jul 9 Misc/
Drwxrwxr-x 9 netsw users 512 Aug 1 Network/
Drwxrwxr-x 2 netsw users 512 Jul 9 Office/
Drwxrwxr-x 7 netsw users 512 Jul 9 SoftEng/
Drwxrwxr-x 7 netsw users 512 Jul 9 System/
Drwxrwxr-x 12 netsw users 512 Aug 3 Typesetting/
Drwxrwxr-x 10 netsw users 512 Jul 9 X11/


In July 1996, I decided to publish my favorites through a beautiful Web interface. "Beautiful" means to provide an interface to directly browse the entire directory structure without making any changes to this structure-or even placing CGI scripts at the top of the structure. Why? Because this structure can be accessed by FTP, and I do not want any Web or CGI components.

Solution:
This solution is divided into two parts: the first part is the CGI script set used to create all directory pages in idle time. I put them in/e/netsw/. www/, as follows:

Code:
-Rw-r -- 1 netsw users 1318 Aug 1. wwwacl
Drwxr-xr-x 18 netsw users 512 Aug 5 DATA/
-Rw-1 netsw users 372982 Aug 5 LOGFILE
-Rw-r -- 1 netsw users 659 Aug 4 TODO
-Rw-r -- 1 netsw users 5697 Aug 1 netsw-about.html
-Rwxr-xr-x 1 netsw users 579 Aug 2 netsw-access.pl
-Rwxr-xr-x 1 netsw users 1532 Aug 1 netsw-changes.cgi
-Rwxr-xr-x 1 netsw users 2866 Aug 5 netsw-home.cgi
Drwxr-xr-x 2 netsw users 512 Jul 8 netsw-img/
-Rwxr-xr-x 1 netsw users 24050 Aug 5 netsw-lsdir.cgi
-Rwxr-xr-x 1 netsw users 1589 Aug 3 netsw-search.cgi
-Rwxr-xr-x 1 netsw users 1885 Aug 1 netsw-tree.cgi
-Rw-r -- 1 netsw users 234 Jul 30 netsw-unlimit.lst



The DATA/subdirectory contains the preceding directory structure, that is, the actual net. sw, which is automatically updated by rdist when necessary. The legacy problem in the second part is: how to connect these two structures into a smooth and visualized URL tree? I hope that the user will not feel the existence of the DATA/directory when running the appropriate CGI script and using various URLs. The solution is as follows: first, I put the following configuration in the configuration file for the directory in DocumentRoot on the server, and rewrite the published URL/net. sw/as the internal path/e/netsw:

Code:
RewriteRule ^ net. sw $ net. sw/[R]
RewriteRule ^ net. sw/(. *) $ e/netsw/$1

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.