【问题标题】:Using RewriteRule to redirect any request to PHP script without external redirects使用 RewriteRule 将任何请求重定向到 PHP 脚本而无需外部重定向
【发布时间】:2016-08-24 22:56:31
【问题描述】:

我使用WampServer (Apache/PHP/MySQL),我的www 目录中有以下文件:

  • www [目录]
    • test [目录]
    • index.php [文件]
    • .htaccess [文件]

我想将所有 URI 请求作为参数传递给index.php,而不会导致任何外部重定向(客户端浏览器中的位置更改)。所以我尝试使用以下规则:

RewriteRule ^(.*)$ index.php?path=$1 [nocase,qsappend,end]

这很好用,除非 URI 是 /test,在这种情况下,外部重定向会将位置更改为:

/test/?path=test

即使存在与 URI 同名的目录,如何确保外部重定向不会发生?


预期结果:

  • 原始 URI 请求:/test
  • 内部重定向:index.php?path=test
  • 外部重定向:无

实际结果:

  • 原始 URI 请求:/test
  • 内部重定向:index.php?path=test
  • 外部重定向:/test/?path=test

理想的解决方案是通用的,不仅适用于test 目录,还适用于任何目录。

【问题讨论】:

  • 将代码放入虚拟主机See here for how to
  • @RiggsFolly 我刚刚尝试过,但没有任何区别。

标签: apache mod-rewrite url-rewriting wampserver


【解决方案1】:

好的,问题似乎是mod_rewritemod_dir 之间的冲突。可能的解决方案是:

  1. 禁用mod_dir
  2. Put this to .htaccess:

    <IfModule mod_dir.c>
         DirectorySlash Off
    </IfModule>
    

有关mod_rewritemod_dir 如何交互的更多详细信息,this answer has some excellent info。为什么需要外部重定向是explained in docs (Trailing Slash Problem)


相关链接

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    相关资源
    最近更新 更多