Apache rewrite 部分细节
来源:互联网 发布:释放内存软件 编辑:程序博客网 时间:2024/06/15 22:08
多个.htaccess匹配的优先级
网站根路径下有一个.htaccess
,根目录下的文件夹test
中也有一个.htaccess
,那么,访问http://localhost/test/xyz
会有什么结果呢(test下不存在名为xyz的文件),或者说如果两个.htaccess
都会对错误的访问进行处理,谁的优先级更高?
根路径下:
RewriteEngine OnRewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -dRewriteRule ^.*$ - [L]RewriteRule ^.*$ https://baidu.com [R=301,L]
test路径下:
RewriteEngine OnRewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -dRewriteRule ^.*$ - [L]RewriteRule ^.*$ https://bing.com [R=301,L]
测试结果是 test
路径下的.htaccess
起效了,也就是在浏览器上访问http://localhost/test/xyz
实际上跳转到了Bing
。当我把test
下的.htaccess
移除后则会跳转到Baidu
.
罗列一些细节
$N
匹配的是RewriteRule 中正则匹配的内容(RewriteRule backreferences),而%N
匹配的是RewriteCond中正则匹配的内容(RewriteCond backreferences),而\N
则是正则表达式中原生的反向引用- 有时候出现的
::
并不是特殊的符号,只是方便使用的分隔符而已,喜欢的话可以用##
等替代12 RewriteCond TestString CondPattern
中,TestString
可以是以下内容构成的:- RewriteRule backreferences
- RewriteCond backreferences
- RewriteMap expansions 3
- Server-Variables 4
RewriteRule Pattern Substitution [flags]
中,pattern匹配的是什么?就我的理解来说,如果访问的链接是http://localhost:80/test/hello/hahahaha?x=100
,- 如果它由网站根目录下的
.htaccess
来处理,它匹配的内容就是test/hello/hahahaha
; - 如果它由
根目录/test/.htaccess
来处理,它匹配的内容就是hello/hahahaha
What is matched?5
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. “/app1/index.html”).
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that led the server to the current RewriteRule (e.g. “app1/index.html” or “index.html” depending on where the directives are defined).
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
In any case, remember that regular expressions are substring matches. That is, you don’t need the regex to describe the entire string, just the part that you wish to match. Thus, using a regex of . is often sufficient rather than .*, and the regex abc is not the same as ^abc$.
- 如果它由网站根目录下的
以后再补充
- What Double Colon does in RewriteCond? ↩
- What does ::$1 mean in an htaccess? ↩
- http://httpd.apache.org/docs/current/rewrite/rewritemap.html ↩
- http://httpd.apache.org/docs/current/mod/mod_rewrite.html#RewriteCond ↩
- http://httpd.apache.org/docs/current/mod/mod_rewrite.html#RewriteRule ↩
0 0
- Apache rewrite 部分细节
- apache rewrite
- apache rewrite
- Apache Rewrite
- apache rewrite
- Apache rewrite
- apache rewrite
- Apache rewrite
- Apache--Rewrite
- apache rewrite
- apache rewrite
- Apache 配置 rewrite
- apache Rewrite 二级域名设定
- apache Rewrite 二级域名设定
- Apache rewrite 资料
- Apache rewrite规则写法
- Apache rewrite例子集合
- Apache URL rewrite规则
- codeforces 711E 勒让德定理 逆元
- Git 命令与基本操作
- Glide用法---学习笔记
- 操作系统服务:OS模块
- Web服务精讲–搭个 Web 服务器(二)
- Apache rewrite 部分细节
- 忆高中阶段---------最充实的时光
- Java中String类通过new创建和直接赋值字符串的区别
- ACdream1726-A Math game
- 笔试题--求幸运数个数
- ios应用生命周期和视图生命周期
- android中的style部分属性值介绍
- Hanoi问题——解惑自己
- samba 配置与安装