mod_rewrite将多个目录转换为php文件
-
03-07-2019 - |
题
RewriteEngine on
RewriteRule ^/(dir1|dir2|dir3)/(.*)$ /targetfile.php [R,L]
http://www.somesite.com/dir1
- &gt; <代码> http://www.somesite.com/targetfile.php 代码>结果
http://www.somesite.com/dir2
- &gt; <代码> http://www.somesite.com/targetfile.php 代码>结果
http://www.somesite.com/dir3
- &gt; <代码> http://www.somesite.com/targetfile.php 代码>
从我在网上看到的情况来看,这应该有效。不幸的是,它不会。有什么见解吗?
解决方案
如果要在.htaccess文件中使用它,请从模式中删除前导斜杠。要仅匹配完整路径段,您必须稍微改变表达式。
所以试试这个:
RewriteEngine on
RewriteRule ^(dir1|dir2|dir3)(/|$) targetfile.php [R,L]
其他提示
我不相信正斜杠是必要的,除非你想限制它在“dir1”之后要求尾部斜杠。
尝试: RewriteRule ^(dir1 | dir2 | dir3)$ targetfile.php [QSA,L]
我认为问题在于正则表达式要求在目录名后面加斜杠(例如/ dir1 /),但在示例中省略了最后一个斜杠( http://www.somesite.com/dir1 没有斜杠。)
我认为你可以尝试使用
RewriteRule ^/(dir[1-3]) /targetfile.php [R,L]
不隶属于 StackOverflow