用了好几次rewrite,用的次数不是非常多,每次都忘,都得又一次上网上找一堆现看,总结一下,以免以后忘了
=====================分隔符=====================
注意下面几点:
1、载入rewrite模块,LoadModule rewrite_module modules/mod_rewrite.so
2、假设是用vhost里配置,则在vhost里打开RewriteEngine on(htaccess文件配置的方式没实用过)
用过的配置:
场景一:当域名是aaa.bbb的时候。跳转到aaa.ccc.bbb这个域名
RewriteEngine on
RewriteCond %{HTTP_HOST} ^aaa.bbb$ [NC]
RewriteRule ^/(.*)$ http://aaa.ccc.bbb/$1 [R=301,L,NC]
%{HTTP_HOST}匹配域名,^表示匹配開始,$表示匹配结束,各自是匹配字符串的左右界,能够不同一时候出现。.表示转义,仅仅匹配.。否则就变成随意单个字符了
NC(no case) 不区分大写和小写,
.是随意单个字符,.*就是随意数量的随意字符,()是子字符串。所以^/(.*)$表示域名后随意路径,这里推測。应该能够省略$,这里的.,没实用转义,是由于这部分不是正则,$1匹配的是第一个()里的内容。R=301表示永久跳转。
场景二:把/testt/这个请求。转到/image/下,可是/testt/xxxx。不发生跳转
RewriteEngine on
RewriteRule ^/testt/$ http://localhost:8000/image/ [R=301,L,NC]
对于query string的操作,下面引自官方文档
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected
into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA]
flag.
=====================分隔符=====================
參考资料:
正則表達式中各种字符的含义 http://www.cnblogs.com/afarmer/archive/2011/08/29/2158860.html
apache的rewrite规则使用说明 http://www.jb51.net/article/48780.htm
13个有用的Apache Rewrite重写规则 http://www.jb51.net/article/47907.htm