mod_rewrite .htaccess 仅匹配文件,也需要目录匹配
我正在尝试将所有流量重新写入 old_site 文件夹,
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+) old_site/$1/$2 [NC,QSA,L]
工作正常
/blog/profile/index.php OR /blog/posts.php
但为什么不匹配
/blog/login/ or /blog/login
我是否需要指定单独的规则来处理带有或不带有尾随斜杠/的目录?
编辑:
根据 slugonamission 我添加了 -d 指令
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*) old_site/$1/$2 [NC,QSA,L]
它适用于 /blog/login/ 但不适用于 /blog/login ,不知何故没有尾随斜杠,它不被认为是一个目录?有没有办法修改当前规则,或者我是否需要新规则来添加斜杠?
I'm trying to re-write all traffic to an old_site folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+) old_site/$1/$2 [NC,QSA,L]
Works fine for
/blog/profile/index.php OR /blog/posts.php
But why doesn't it match
/blog/login/ or /blog/login
Do I need to specify a separate rule to handle directories with and without trailing slash/ ?
EDIT:
As per slugonamission I've added the -d directive
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*) old_site/$1/$2 [NC,QSA,L]
It's works for /blog/login/ however not for /blog/login , somehow without the trailing slash, it's not thought of as a directory? Is there a way to modify the current rule or do I need a new one to add the slash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你几乎做到了!
这是应该有效的:
请告诉我它是否有效
:)
顺便说一下,这是我最喜欢的检查正则表达式的工具:
http ://www.quanetic.com/Regex (不要忘记选择 k ereg(POSIX) 而不是 preg(PCRE)!)
You almost did it!
Here's what should work:
Please tell me if it worked
:)
By the way, here's my favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choosek ereg(POSIX) instead of preg(PCRE)!)
你快到了!您还需要单独处理目录,这是通过
-d
条件完成的。You're almost there! You also need to handle directories separately, which is done with the
-d
condition.经过更多研究,以下模式符合我的需要:
/blog/profile/index.php
/blog/posts.php
包括带或不带尾部斜杠的目录:
/blog/login/
/博客/登录
With some more research the following pattern matches what I need:
/blog/profile/index.php
/blog/posts.php
Including directories with and without trailing slashes:
/blog/login/
/blog/login