Apache rewrite rules for common applications (rewrite)

2010-04-01  来源:本站原创  分类:Internet  人气:263 

This article comes from: http://www.linuxforum.net of: Miss A Pavilion Jephe wu (2001-09-05 08:00:00)

This paper provides how to use Apache rewrite rules to address some common methods of URL rewriting problem to the user through a common example of some of the basic methods to use rewrite rules and clues.

2: Why do I need to use rewrite rules?
A website on the internet if long-term need to provide services, must be continually updated and maintained, such as temporary transfer to other server maintenance, re-organized directory structure, changing to a new URL or domain name changes, etc.
In order to allow customers will not be affected in any way, the best way is to use Apache Rewrite Rule (rewrite rules).

3: The role of the scope of rewrite rules
1) can be used in the main configuration file httpd.conf in Apache
2) You can use the definition in the httpd.conf in the virtual host configuration
3) You can use the cross in the base directory configuration file. Htaccess in the

4: Rewrite the rules of application conditions only when the user requests WEB eventually directed to a WEB server Apache Taiwan background, then this sets WEB server to accept incoming request, according to the request is the main configuration file configuration or virtual host, then under user requests in the browser
URI rewriting rules to match the request and the actual path to match. Htaccess rewrite rules in. Finally the contents of the request back to the user, the response may have two:

1) The browser requests the content of external redirection (Redirect) to another URL.
Let the browser again to the new URI request (R = 301 or R = 302, temporary or permanent redirect)
Such as: a website with regular URL and alias URL, URL of the Alias to redirect to a regular URL, or Web site put the change into the new domain redirect the old domain to new domain (Redirect)

2) may also be requested by the Apache internal sub-agents to generate new content back to the customer [P, L]
This is after the Apache URI rewriting internal according to internal content through a proxy module and return the contents of the request to the client, but does not know the client browser, the browser will not be rewritten URI. However, the actual content is Apache rewrite rules under the URI received.
Such as: the corporate firewall running Apache rewrite rules to begin such a proxy, the proxy on the internal network segment on the WEB server requests.

5: How to rewrite the rules work?
We assume that the time of compiling the Apache mod_rewrite module compiled, make sure your httpd.conf has
LoadModule rewrite_module libexec / mod_rewrite.so
And there in Addmodule
Addmodule mod_rewrite.c
You can use the rewrite rules.
When the external request to Apache, Apache rewrite rules is called to rewrite the definition of specified by the user's browser requests
URI, final URI to be rewritten if redirected, then sent by the browser to make another request; If it is after the agency put the rewriting request URI to the proxy module final content (Content), the final return to contents to the browser.

6: When to Use. Htaccess rewrite the rules of the definition?
If you are content to your site where the server does not have administrator rights, or your website hosted on the ISP's server, so conditions, you can not rewrite the main configuration file, but you can on your WEB site content is located directory has write permissions, then you can set your own. htaccess
File to achieve the same purpose. But you need to identify the main configuration file on your website directory where the definition of the following:

Options Indexes FollowSymLinks
AllowOverride all

Otherwise your. Htaccess will not work.

7: The example assumes that Apache is compiled installed in the host in / usr / local / apache / directory the following, we compiled a rewrite and proxy modules.

1) hide a directory under Apache, make any request to the directory redirects to another file.

a> httpd.conf Realization

We put some of the following to / usr / local / apache / conf / httpd.conf

options Indexes followsymlinks
allowoverride all
rewriteengine on
rewritebase /
rewriterule ^(.*)$ index.html.en [R = 301]

Note: rewriteengine on to rewrite the engine switch, if set off, then any definition of rewrite rules will not be applied, the switch has the added advantage that if the temporary removed to rewrite the rules changed to off and then restart Apache you can, do not have to rewrite the rules of Article 1 the following comment.
rewritebase /'s role is, if the definition in the following rewriterule be rewritten after some (here, the file name index.html.en) not preceded by /, is a relative directory, relative to the back of this rewritebase definition is / usr / local / apache / htdocs / index.html.en, or, if here is not rewritebase / this one, were re-written, is clearly not correct.

But here we can not rewritebase /, instead of
rewriteengine on
rewriterule ^(.*)$ / index.html.en [R = 301]
rewriteengine on
rewriterule ^(.*)$ [R = 301]

b>. htaccess Realization

Let's put some of the following to httpd.conf

options Indexes followsymlinks
allowoverride all

Then put the following section to / usr / local / apache / htdocs / manual / .htaccess in
rewriteengine on
rewritebase /
rewriterule ^(.*)$ index.html.en [R = 301]

Note: the file. Htaccess does not need any changes made to restart Apache.

Q: If this manual directory redirects to the user's own home directory jephe it?
With the following. Htaccess program.
rewriteengine on
rewritebase / ~ jephe /
rewriterule ^(.*)$ $ 1 [R = 301]

Manual directory is on request of any document to be redirected to the same file ~ jephe directory requests.

2) conversion www.username.domain.com the request for the home page for the username
www.domain.com / username

For HTTP/1.1 requests include a Host: HTTP header, we can use the following set of rewriting rules
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

rewritecond conditions of rewrite rules, when the conditions meet the definition of the back until after the application of the following rewrite rules
rewritecond a variety of variables, please refer to the documentation.

3) rewrite the rules on the firewall proxy server within the network segment on the request.


servername www.domain.com
rewriteengine on
proxyrequest on
rewriterule ^/(.*)$ $ 1 [P, L]

Note: When an external browser requests www.domain.com to the IP address is resolved when the, Apache surrender
mod_rewrite processing into $ 1 and then received by the proxy module mod_proxy content is sent back to the user's browser.

4) The basic pre-set conversion table rewriting rewritemap MAP

Conversion www.domain.com/ (countrycode) / anypath to the Map table provides the URI, the above is the definition of 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 (.*)::([ az] +) $ $ (sitemap: $ 2 | http://hijk/) [R = 301, L]

File / usr / local / apache / conf / rewrite.map reads as follows:

sg http://abcd/
sh http://efgh/

Note: When a user requests to be rewritten when http://www.domain.com/sg/anypath
When you need to debug, please use the joint rewritelog and rewriteloglevel 9, 9 for the most get the most of debugging information that the minimum is 1, the minimum debugging information, the default is 0, no debugging information.
sitemap syntax is $ (sitemap: LookupKey | Defaultvalue), some books written to the% $ is wrong.


  • Apache rewrite rules for common applications (rewrite) 2010-04-01

    This article comes from: http://www.linuxforum.net of: Miss A Pavilion Jephe wu (2001-09-05 08:00:00) This paper provides how to use Apache rewrite rules to address some common methods of URL rewriting problem to the user through a common example of

  • apache rewrite rules 2010-03-12

    This paper provides how to use Apache rewrite rules to address some common methods of URL rewriting problem to the user through a common example of some of the basic methods to use rewrite rules and clues. 2: Why do I need to use rewrite rules? A sit

  • How to configure the IIS environment Rewrite Rules 2011-01-04

    Note: Transfer from the Internet. Static URL can improve search engine crawlers, open the Web server feature requires a corresponding increase of Rewrite rules, and will slightly increase the burden on the server. This tutorial explains how to config

  • The rewrite rules Apache2 does not work 2010-08-04

    Attention 1. LoadModule rewrite_module modules / mod_rewrite.so 2. <Directory "E:/Apache2.2/htdocs"> # # Possible values for the Options directive are "None", "All", # Or any combination of: # Indexes Includes FollowSym

  • Use Apache MINA developing high-performance network applications 2010-06-29

    This will be a simple greeting program HelloServer MINA infrastructure to introduce the same time shows how to develop web applications using MINA. <! - START RESERVED FOR FUTURE USE INCLUDE FILES - ><!-- include java script once we verify teams

  • apache configure a virtual machine applications 2010-12-03

    apache can configure their own virtual machines to achieve load balancing. Can be applied to a machine configured with two network cards, or multiple domain names can be used APACHE's own virtual machine implementation. First configure the httpd.conf

  • Intent start using common applications and services 2010-11-29

    The following ATAAW.COM lists some commonly used in the Android Intent to start the service, the time when the implementation of startActivity, Android will be based on the information Intent bound to find the most suitable to start the program provi

  • url rewriting rewrite nginx server parameters and examples 2011-09-23

    This Transfer: http://www.cnblogs.com/cgli/archive/2011/05/16/2047920.html Recently installed in the VPS on trying to configure a Web site, VPS installed LNMP (Linux + Nginx + MySQL + php) rules in the configuration weight problems often encountered

  • Configure Nginx to Rewrite 2010-06-08

    Nginx's Rewrite After inspection and testing of online and found Nginx of Rewrite Rules and Apache's Rewite rule difference is not great, almost directly. For example, in Apache, he wrote the rules rewrite ^ / ([0-9] (5)). html $ / viewthread.php? ti

  • 基于jetty 自制Java Web Server 容器 2010-04-13

    1.main class 如下: public final class Bootstrap { //~ Static fields/initializers ============================================= private static final String SHUTDOWN = "SHUTDOWN"; private static final String RELOAD = "RELOAD"; //~ Instance

  • Nginx common Rewrite (pseudo-static rules) 2010-06-04

    I believe most now use Linux VPS friends are using the rapid spread of Nginx, today the most common sort of PHP programs Rewrite (pseudo-static rules). Wordpress: location / ( index index.html index.php; if (-f $ request_filename / index.html) ( rewr

  • Reprinted apache rewrite rule 2010-06-08

    Reprinted http://www.javaeye.com/topic/579481 Apache Rewrite Rules Xiangjie: http://slj.me/2009/04/apache-rewrite-regular/ 1, Rewrite Rule Description: Rewirte main function is to realize the URL of the jump, it is based on Perl regular expression la

  • Apache Rewrite to achieve a URL and domain name jump jump 2011-07-28

    Rewirte main function is to realize the URL of the jump, it is based on Perl regular expression language. Can be based on server-level (httpd.conf) and per-directory (. Htaccess) in two ways. If you want to use the rewrite module, you must first inst

  • apache rewrite the URL in Substitution | RewriteRule directive 2010-05-07

    Rewrite the rules of the Substitution Pattern when match the original URL and when used as a substitute (or replace) the string. In addition to plain text, you can also include: Pattern reference on the reverse ($ N) The final match of the RewriteCon

  • In Nginx configure wordpress URL rewrite the general rules 2010-06-25

    Before the introduction of URL rewrite rules may be multi-layer structure of the catalog to support the poor, so I find the following simpler formulation of this can be common directories and files. Quote if (!-e $ request_filename) ( rewrite ^(.+)$

  • Apache Rewrite URL Jump basic and pseudo-static 2010-08-06

    Rewirte main function is to realize the URL of the jump, Rewirte code is based on Perl regular expression language. Based on the server level (httpd.conf) and directory level (. Htaccess) in two ways, and we generally use the Apache directory level (

  • Apache Rewrite实现URL的301跳转和域名跳转 2014-02-01

    Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基于服务器级的(httpd.conf)和目录级的 (.htaccess)两种方式 如果要想用到rewrite模块,必须先安装或加载rewrite模块.方法有两种一种是编译apache的时候就直接安装rewrite模块,别一种是编译apache时以DSO模式安装apache,然后再利用源码和apxs来安装rewrite模块. 基于服务器级的(httpd.conf)有两种方法,一种是在httpd.conf的全局下直接利

  • Apache Rewrite实现URL的跳转和域名跳转 2014-12-30

    1.Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基 于服务器级的(httpd.conf)和目录级的 (.htaccess)两种方式.如果要想用到rewrite模块,必须先安装或加载rewrite模块.方法有两种一种是编译apache的时候就直接 安装rewrite模块,别一种是编译apache时以DSO模式安装apache,然后再利用源码和apxs来安装rewrite模块. 基于服务器级的(httpd.conf)有两种方法,一种是在httpd.conf的全局

  • Apache Rewrite 规则详解 2015-05-12

    1.Rewrite规则简介 Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基于服务器级的(httpd.conf)和目录级的 (.htaccess)两种方式.如果要想用到rewrite模块,必须先安装或加载rewrite模块.方法有两种一种是编译apache的时候就直接 安装rewrite模块,别一种是编译apache时以DSO模式安装apache,然后再利用源码和apxs来安装rewrite模块. 基于服务器级的(httpd.conf)有两种方法,一种是在ht

  • Write apache rewrite in the next ubuntu 2010-09-04

    First of all, make sure your apache have it installed. Then this sudo ln-s / etc/apache2/mods-available/rewrite.load / etc/apache2/mods-enabled/rewrite.load make rewrite Module load module. Here involves a relationship between available and enable th