【问题标题】:Internal redirect from exisiting folder to document root file with mod_rewrite使用 mod_rewrite 从现有文件夹内部重定向到文档根文件
【发布时间】:2018-04-29 04:12:21
【问题描述】:

我想创建一个 internal 重写,它从文档根目录传递一个 .php 文件。

以下示例显示了它应该如何工作:

https://example.com/test 应该加载文件https://example.com/template.php?a=test https://example.com/test1 应该加载文件https://example.com/template.php?a=test1

我已经写了一个 RewriteRule:

RewriteRule ^([a-z]+)$ template.php?a=$1 [NC,END]

此规则适用于一种情况。如果文件夹存在,在本例中为“test”,它将导致重定向循环。我认为这种行为的原因是 Web 服务器导航到“/test”,然后查找“template.php”。由于没有文件(因为“template.php 在文档根目录中”)重定向失败。

如何解决此问题?如果模式匹配,则无论文件夹是否存在,都应该始终进行内部重定向。

【问题讨论】:

  • 你还有什么规定吗?
  • 不,我禁用了所有这些。唯一启用的是Options -Indexes。但是,如果我在没有此“规则”的情况下访问该页面,它只会向我显示 /test 的内容并添加“/?a=test”。
  • 这可能是由于您的其他规则在它们处于活动状态时您的浏览器缓存了它们。尝试清除浏览器缓存。
  • 没有帮助。问题是在 /test 文件夹中而不是在文档根目录中查找 template.php。

标签: apache .htaccess redirect mod-rewrite url-rewriting


【解决方案1】:

this 问题的帮助下,即使文件夹存在,我也想出了如何重写:我将DirectorySlash Off 添加到我的.htaccess 中。

来自阿帕奇documentation

DirectorySlash 指令确定 mod_dir 是否应该修复指向目录的 URL。

通常,如果用户请求的资源没有尾部斜杠, 指向一个目录,mod_dir 将他重定向到同一个目录 资源,但带有斜杠 [...]

此外,我添加了另一个重写规则,它删除了最后一个斜杠。

RewriteRule ^(.*)/$ /$1 [L,R=301]

现在以下工作:

https://example.com/test 加载文件https://example.com/template.php?a=test

https://example.com/test/ 重定向到 https://example.com/test 加载文件https://example.com/template.php?a=test

【讨论】:

    猜你喜欢
    • 2019-04-30
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    相关资源
    最近更新 更多