Rewrite open and rule usage instructions for Apache under Windows

Source: Internet
Author: User

System environment: Windows Apache 2.2

Load Rewrite module:

Found in httpd.conf in the Conf directory

LoadModule Rewrite_module modules/mod_rewrite.so

This sentence, remove the front annotation symbol "#", or add this sentence.

Allow the ". htaccess" file to be used in any directory and change "allowoverride" to "all" (Default "None"):

The code is as follows Copy Code

# AllowOverride Controls What directives may is placed in. htaccess files.
# It can be ' all ', ' None ', or any combination of the keywords:
# Options FileInfo authconfig Limit
#
AllowOverride All

You cannot create a ". htaccess" file directly under Windows System, you can use "echo a>. htaccess" at the command line to create it, and then use Notepad to edit it.

Rewrite logo

R[=code] (Force redirect) force external redirection

Forces a URL to be redirected to an external string, plus the http://thishost[:thisport]/prefix. If code is not specified, the default 302 HTTP status code is used.

F (force URL to is forbidden) disables the URL and returns a 403HTTP status code.
G (force URL to is gone) forces the URL to gone and returns a 410HTTP status code.
P (force proxy) forces the use of proxy forwarding.
L (last rule) indicates that the current rule is the final one and stops parsing the rule after it is overridden.
N (next round) again runs the rewrite process from the first rule.
C (chained with next) is associated with the next rule
If the rule match is handled normally, the flag is invalid and if it does not match, all of the following associated rules are skipped.
T=mime-type (Force MIME type) force MIME type
NS (used only if no internal sub-request) is used only for not internal child requests
NC (no case) is case-insensitive
QSA (query string append) Append request string
NE (no URI escaping of output) does not escape special characters in output

For example: rewriterule/foo/(. *)/bar?arg=p1%3d$1 [R,ne] will be able to correctly convert/foo/zoo to/bar?arg=p1=zed

PT (passes through to next handler) passed to the next processing

For example:

The code is as follows Copy Code

Rewriterule ^/abc (. *)/def$1 [PT] # will be given to/def rule processing
Alias/def/ghi
S=num (skip next rule (s) skips the NUM bar rules www.111cn.net
E=var:val (Set environment variable) set environment variable

Server variable when rewrite:

The code is as follows Copy Code

HTTP headers:http_user_agent, Http_referer, Http_cookie, Http_host, http_accept
Connection & Request:remote_addr, query_string
Server Internals:document_root, Server_port, Server_protocol
System Stuff:time_year, Time_mon, Time_day

Description of the rewrite rule expression:

. Match any single word character
[chars] matching string: chars
[^chars] does not match string: chars
TEXT1|TEXT2 selectable string: Text1 or Text2
? Match 0 to 1 characters
* Match 0 to more characters
+ match 1 to more characters
^ String Start flag
$ string End Flag
N Escape Character Flag

Reverse reference $N variable invocation for matching in rewriterule (0 < = N <= 9)
Reverse reference%N for the last matching variable call in Rewritecond (1 <= N <= 9)


Rewritecond Marker

' Nocase| NC ' (no case) ignores size
' Ornext| or ' (or next condition) logic or, you can match multiple rewritecond conditions at the same time
Rewriterule the applicable identifier
' Redirect| R [=code] ' (Force redirect) forced overrides to an external turn based on HTTP (note URL changes) such as: [R=301,l]
' Forbidden| F ' (Force URL to is forbidden) overridden to prohibit access
' Proxy| P ' (force proxy) overrides the HTTP path that is accessed through the proxy
' Last| L ' (last rule) The final rewrite rule flag, if matched, no longer executes subsequent rules
' Next| N ' (next round) loops the same rule until the match is not met
' Chain| C ' (chained with next rule) if the rule is matched, continue with the following rules with the chain flag.
' Type| T=mime-type ' (Force MIME type) specifies MIME type
' Nosubreq| NS ' (used only if no internal sub-request) skipped if it is an internal child request
' Nocase| NC ' (no case) ignores size
' Qsappend| QSA ' (query string append) append query strings
' Noescape| NE ' (no URI escaping of output) prevents characters in a URL from automatically escaping into a%[0-9]+ form.
' Passthrough| PT ' (pass through to next handler) applies the rewrite results to Mod_alias
' Skip| S=num ' (skip next rule (s)) skip the following few rules
' Env| E=var:val ' (Set environment variable) add environment variables

The simple application of the Apache rewrite module:
All of the rewrite rules are based on Perl-style regular expressions, and the following basic examples can be used to write code that matches their jump requirements.

1, request to jump

The purpose is to jump to another domain name if the request is a. jsp file.

For example: Visit www.mrlon.net/a.php jump to www.111cn.net/b.php page, Access www.mrlon.net/news/index.php to www.111cn.net/news/index.php page

Note: It is not using meta or JavaScript in HTML technology, because www.mrlon.net/a.php this file does not exist, with the rewrite module in the Apache2.2 server.

Modify the. htaccess or apche configuration file httpd.conf file, add the following

The code is as follows Copy Code

Rewriteengine on
#开启Rewrite模块
Rewriterule (. *). php$ http://www.111cn.net/$1.jsp [R=301,L,NC]
#截获所有. JSP request, jump to http://www.111cn.net/plus the original request plus. php. r=301 is 301 jump, l for rewrite rule to terminate, NC is case-insensitive

2, Domain name jump

If the request is for all URLs under 111cn.net, jump to Www.111cn.net

The code is as follows Copy Code

Rewriteengine on
#开启Rewrite模块
Rewritecond%{remote_host} ^old.studenthome.cn$ [NC]
#针对host为111cn. NET host to do processing, ^ for start character, $ for end character
Rewriterule (. *) http://www.111cn.net/$1 [R=301,L,NC]

3, anti-theft chain

If the images on this site do not want to be called by other sites, you can add the following in the. htaccess or Apche profile httpd.conf file

The code is as follows Copy Code

Rewriteengine on
#开启Rewrite模块
Rewritecond%{http_referer}!^$
#如果不是直接输入图片地址
Rewritecond%{http_referer}!img.mrlon.net$ [NC]
#且如果不是img. mrlon.net all child domain names called by
Rewritecond%{http_referer}!img.mrlon.net/(. *) $ [NC]
Rewritecond%{http_referer}!zhuaxia.com [NC]
Rewritecond%{http_referer}!google.com [NC]
Rewritecond%{http_referer}!google.cn [NC]
Rewritecond%{http_referer}!baidu.com [NC]
Rewritecond%{http_referer}!feedsky.com [NC]
Rewriterule (. *). (jpg|jpeg|jpe|gif|bmp|png|wma|mp3|wav|avi|mp4|flv|swf) $ http://clin003.com/err.jpg [R=301,L,NC]
#截获所有. jpg or. jpeg ... Request, jump to http://clin003.com/err.jpg hint wrong picture, note: This picture cannot be under the original domain name, also cannot be in the folder that the. htaccess file is effectively controlled

4, do not need to define the. htaccess file

Add on apache2confhttpd.conf last line

The code is as follows Copy Code

Rewriteengine on
Rewriterule ^ (. *)-htm-(. *) $ $1.php?$2

Example:

The code is as follows Copy Code

Rewriteengine on
Rewritecond%{http_user_agent} ^msie [Nc,or]
Rewritecond%{http_user_agent} ^opera [NC]
Rewriterule ^.*-[f,l] here "-" means no replacement, browsers for IE and opera visitors will be barred from accessing.

Example:

The code is as follows Copy Code

Rewriteengine on
Rewritebase/test
Rewritecond%{request_filename}.php-f
Rewriterule ([^/]+) $/test/$1.php
#for Example:/test/admin =>/test/admin.php
Rewriterule ([^/]+). html$/test/$1.php [L]
#for Example:/test/admin.html =>/test/admin.php

Restricted directories can only display pictures

  code is as follows copy code

< Ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond%{request_filename}!^.*. ( GIF|JPG|JPEG|PNG|SWF) $
rewriterule. *$–[f,l]
</ifmodule>

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.