0

我是 Apache 重写 URL 的初学者。

我在重写 URL 时遇到问题。

我想将 www.test.com/abc/index.php?val=123 重写为 www.test.com/abc/123/index.php

注意:abc 是文件夹

RewriteEngine on
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)/([0-9]+)/index.php $1/index.php?val=$2 [QSA,L]
4

2 回答 2

0

您在 RewriteRule 中的正则表达式没有意义。此外,您不能在 RewriteRule 中引用部分查询字符串,因此您必须另外使用 RewriteCond 来捕获查询参数。

试试这个:

RewriteEngine on
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteCond  %{QUERY_STRING} val=([0-9]+)
RewriteRule  ^(.+)/index.php $1/%1/index.php [L]

让 apache 本身帮助您调试表达式总是一个好主意:使用日志记录功能:阅读两个命令的文档RewriteLogRewriteLogLevel. 他们转储采取的每一步,并允许了解算法内部实际发生的情况。

所需的所有信息都在精美的文档中提供。真的值得努力...

于 2012-10-20T06:28:24.660 回答
0

如果您在 htaccess 文件中编写此规则,请添加RewriteBase指令

RewriteEngine On
RewriteBase /
于 2012-10-20T06:28:40.320 回答