`
yangyou230
  • 浏览: 1649718 次
文章分类
社区版块
存档分类

Apache mod_rewrite 小结

 
阅读更多

mod_rewrite

Apache HTTP Server

Enable htaccess on Apache

1. uncomment this line in httpd.conf:

LoadModule rewrite_module modules/mod_rewrite.so

2. change directory setting in httpd.conf:

<Directory />

Options FollowSymLinks

AllowOverride All

Order deny,allow

Deny from all

Satisfy all

</Directory>

Configures what features are available in a particular directory

Options 属性:

Indexes 当该目录下没有index.*时则以ftp-style列出该目录下所有文件

Includes Allow server-wide includes

FollowSymLinks 当该目录下软连接的文件/目录链接到外部目录时,仍然可以正常显示。

MultiViews 由一个*.var管理同一网页的多种语言版本,如apache默认主页多种语言的index.html

ExecCGI 允许执行CGI程序

ALL 开启除MultiViews之外的属性

None 禁止所有属性

AllowOverride 是否允许使用.htaccess覆盖某些设定(All None FileInfo AuthConfig Limit)

Order allow,deny : 控制访问权限

Order deny,allow

deny from test.org 拒绝test.org访问

deny from 192.168.0.100 拒绝192.168.0.100访问

allow from 192.168.0.1 允许192.168.0.1 访问

http://httpd.apache.org/docs/2.0/mod/core.html

http://m.oschina.net/bbs/thread/176

authentication

http://www.htaccesstools.com/htaccess-authentication/

.htaccess

Reference:

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

Examples:

http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide.html

http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide_advanced.html

http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html#default-mod-rewrite-hint

http://www.webmasterworld.com/forum92/2524.htm

Tools:

http://cooletips.de/htaccess/

http://www.htaccesstools.com/

usually it is used to transfer from static URL to dynamic URL

#Error pages

ErrorDocument 404 http://www.backbase.com/errors/404.php

RewriteRule directives

last|L' (last rule)

Stop the rewriting process here and don't apply any more rewrite rules. This corresponds to the Perl last command or the break command in C. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules. Remember, however, that if the RewriteRule generates an internal redirect (which frequently occurs when rewriting in a per-directory context), this will reinject the request and will cause processing to be repeated starting from the first RewriteRule.

'next|N' (next round)

Re-run the rewriting process (starting again with the first rewriting rule). This time, the URL to match is no longer the original URL, but rather the URL returned by the last rewriting rule. This corresponds to the Perl next command or the continue command in C. Use this flag to restart the rewriting process - to immediately go to the top of the loop. Be careful not to create an infinite loop!

'nocase|NC' (no case)

This makes the Pattern case-insensitive, ignoring difference between 'A-Z' and 'a-z' when Pattern is matched against the current URL.

'noescape|NE' (no URI escaping of output)

This flag prevents mod_rewrite from applying the usual URI escaping rules to the result of a rewrite. Ordinarily, special characters (such as '%', '$', ';', and so on) will be escaped into their hexcode equivalents ('%25', '%24', and '%3B', respectively); this flag prevents this from happening. This allows percent symbols to appear in the output, as in

RewriteRule /foo/(.*) /bar?arg=P1/%3d$1 [R,NE]

which would turn '/foo/zed' into a safe request for '/bar?arg=P1=zed'.

'redirect|R [=code]' (force redirect)

Prefix Substitution with http://thishost[:thisport]/ (which makes the new URL a URI) to force a external redirection. If no code is given, a HTTP response of 302 (MOVED TEMPORARILY) will be returned. If you want to use other response codes, simply specify the appropriate number or use one of the following symbolic names: temp (default), permanent, seeother. Use this for rules to canonicalize the URL and return it to the client - to translate ``/~'' into ``/u/'', or to always append a slash to /u/user, etc.

Note: When you use this flag, make sure that the substitution field is a valid URL! Otherwise, you will be redirecting to an invalid location. Remember that this flag on its own will only prepend http://thishost[:thisport]/ to the URL, and rewriting will continue. Usually, you will want to stop rewriting at this point, and redirect immediately. To stop rewriting, you should add the 'L' flag.

While this is typically used for redirects, any valid status code can be given here. If the status code is outside the redirect range (300-399), then the Substitution string is dropped and rewriting is stopped as if the L flag was used.

RewriteCond directives

used before RewriteRule directives

# RewriteRule backreferences: These are backreferences of the form $N (0 <= N <= 9), which provide access to the grouped parts (in parentheses) of the pattern, from the RewriteRule which is subject to the current set of RewriteCond conditions..

# RewriteCond backreferences: These are backreferences of the form %N (1 <= N <= 9), which provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions.

# RewriteMap expansions: These are expansions of the form ${mapname:key|default}. See the documentation for RewriteMap for more details.

# Server-Variables: These are variables of the form %{ NAME_OF_VARIABLE } where NAME_OF_VARIABLE can be a string taken from the following list:

HTTP headers: connection & request:

HTTP_USER_AGENT

HTTP_REFERER

HTTP_COOKIE

HTTP_FORWARDED

HTTP_HOST

HTTP_PROXY_CONNECTION

HTTP_ACCEPT

REMOTE_ADDR

REMOTE_HOST

REMOTE_PORT

REMOTE_USER

REMOTE_IDENT

REQUEST_METHOD

SCRIPT_FILENAME

PATH_INFO

QUERY_STRING

AUTH_TYPE

server internals: system stuff: specials:

DOCUMENT_ROOT

SERVER_ADMIN

SERVER_NAME

SERVER_ADDR

SERVER_PORT

SERVER_PROTOCOL

SERVER_SOFTWARE

TIME_YEAR

TIME_MON

TIME_DAY

TIME_HOUR

TIME_MIN

TIME_SEC

TIME_WDAY

TIME

API_VERSION

THE_REQUEST

REQUEST_URI

REQUEST_FILENAME

IS_SUBREQ

HTTPS

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics