【发布时间】:2011-10-16 05:37:37
【问题描述】:
我有一个基于 n PATH_INFO 变量构建内容的索引文件。
例子:
site.com/A/B/n/
应该在以下任一位置使用 index.php:
site.com/index.php?var1=A&var2=B&varN=n
- or -
site.com/index.php/A/B/n/
代替:
site.com/A/B/n/index.php || which doesn't exist ||
到目前为止,我已经尝试了多种变体:
RedirectMatch ^/.+/.*$ /
没有成功。
我在这里有一个不优雅且不可扩展的解决方案:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?p1=$1 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [NC,L]
此解决方案的问题:
- 不优雅且不可扩展,每个子目录都需要手动行
- 使用非字母数字字符(主要是 +、= 和 &)例如失败。 site.com/ab&c/de+f/ (请注意,即使将正则表达式更改为 ^([a-zA-Z0-9_-\+\=\&]+)/?$ 也无济于事,实际上完全出错了)
你能帮忙吗?
【问题讨论】:
-
那么..你更喜欢哪个:
site.com/index.php?var1=A&var2=B&varN=n(需要多个规则)或site.com/index.php/A/B/n/(可以使用单个规则)? -
我都愿意。在平等的情况下,我更喜欢一条线。
标签: .htaccess mod-rewrite apache apache-config