【问题标题】:Who adds a slash at the end of my url?谁在我的网址末尾添加斜杠?
【发布时间】:2011-08-21 19:09:27
【问题描述】:

我正在使用 mod_rewrite 将 /products 重写为 /products.php。我在 /.htaccess

中有这段代码
Options FollowSymLinks
RewriteEngine on

RewriteRule ^([a-zA-z]+)$ /$1.php [PT,L]

不幸的是,我的服务器上还有一个文件夹 /products/

我的问题是,当我尝试访问 http://mydomain.com/products 时,我的请求被重定向到 http://mydomain.com/products/并显示一个错误,因为我没有该目录的索引。

谁在重定向我?阿帕奇,我的用户代理? 如何在不更改文件夹名称或重写规则的情况下防止这种情况发生?

【问题讨论】:

  • 如何使用mod_rewrite?请向我们展示mod_rewrite 规则 - 在某些情况下,现有文件和文件夹优先于 mod_rewrite。

标签: apache mod-rewrite redirect user-agent


【解决方案1】:

您需要查找“DirectorySlash 指令”。

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

http://httpd.apache.org/docs/2.2/mod/mod_dir.html

您也可以尝试在重写规则中添加可选的斜线:

RewriteRule ^([a-zA-z]+)/?$ /$1.php [PT,L]

Trailing slashes problem

【讨论】:

  • 我在 .htaccess 文件中添加了“DirectorySlash Off”,但它仍在重定向。
  • @esel 尝试其他解决方案。也许对你来说更容易。
  • 是的,这与我从 Marms 帖子中得到的想法完全相同。谢谢。
【解决方案2】:

您可能已经在您的 Apache 上启用了MultiViews

【讨论】:

    【解决方案3】:

    如果不是“.something”,每个浏览器都会在您的请求后添加斜杠,因为它认为它是一个文件夹。为避免这种情况,您的重写规则应如下所示:

    重写规则 /products(.*)$ /index.php?page=products

    重写规则 /products /index.php?page=products

    这样,它将重写每个带有“/products”的请求,无论是否带有斜杠。

    唯一的问题是您的文件夹 /products/ 将无法通过 http 请求访问。如果需要,您必须更改文件夹名称或页面名称。

    【讨论】:

    • 谢谢。我可以将我的规则更改为 RewriteRule ^([a-zA-z]+)\/?$ /$1.php [PT,L] 所以只有 /products 和 /products/ 被重写,而不是对文件夹的其他请求。
    猜你喜欢
    • 2010-11-25
    • 2013-08-31
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多