【发布时间】:2014-05-12 14:20:19
【问题描述】:
给定一个目录结构:
/test/
/test/a
/test/a/b
/test/a/b/c
在上面的示例中,每个目录都有一个索引文件 index.php。我在/test/ 中有一个 .htaccess ... 我想避免在每个目录中都有一个 .htaccess。
我需要编写一个重写规则,它将显示最接近输入到系统中的 URL 的 index.php。
例如:
url /test/a/b/1234 should get rewritten to /test/a/b/index.php
url /test/a/b/c/1234 should get rewritten to /test/a/b/cindex.php
url /test/1234 should get rewritten to /test/index.php
我尝试了几种不同的 .htaccess,但所有内容都被重写为 /test/index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
我也试过RewriteRule . ./index.php [L]
如果我将 RewriteBase 设置为子目录之一,它会重写到该基础中的 index.php。但随后一切都被写入该基地。
提前致谢。
更新:
网址长度未知。示例:
url /test/a/b/1234/abcd/this/that/another should get rewritten to /test/a/b/index.php
基本上,我需要知道 .htaccess 从中匹配的目录,以用作重写器的重写库。
【问题讨论】:
-
捕获
1234之前的部分,并将其用于替换...
标签: regex apache .htaccess mod-rewrite