.htaccess从动态网址到静态网址的重写规则(无查询字符串)

时间:2019-04-14 19:04:00

标签: regex .htaccess mod-rewrite url-rewriting

我有一个表单为http://www.example.com/results/index.php的页面。我需要这样做,以便可以从URL http://www.example.com/results/[anystring]访问该表单。也就是说,用户在浏览器地址栏中看到了http://www.example.com/results/[anystring]的URL,但是他们正在从服务器看到页面http://www.example.com/results/index.php的内容。页面的表单将使用“ anystring”部分,如果它在期望的字符串列表中,则将显示该表单,并且“ anystring”将是隐藏字段的值。我知道该怎么做的PHP部分-如果在预期字符串列表中,请使用表单中的“ anystring”。我不知道如何使用动态片段-http://www.example.com/results/[anystring]-从URL进行重写以访问静态页面-http://www.example.com/results/index.php

到目前为止,我发现以下内容无效:

RewriteEngine On
RewriteBase   /results/
Rewrite Rule (\w+) index.php [L]

此外,还有其他网址需要保持不受此网址的影响,例如http://www.example.com/results/path/to/other/files.php

有人可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:2)

您距离很近。您可以在results/.htaccess文件中使用此规则:

RewriteEngine On
RewriteBase /results/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]