【问题标题】:Apache RewriteEngine - Rewrite anything that not exists to index.phpApache RewriteEngine - 将不存在的任何内容重写为 index.php
【发布时间】:2017-08-23 23:52:14
【问题描述】:

我现在真的很挣扎。
首先,我在 Google 和这里​​进行了搜索,但没有找到适合我的解决方案。我现在真的没有太多时间来完全深入研究 Apache 文档以深入了解 RewriteEngine(以后肯定会这样做)。

我需要什么
我需要处理来自我们的 Google Adwords 广告系列的特殊请求,如下所示:http://website.com/something/%first_param%/%second_param%/%third_param% 在我的文档根目录中,我有 index.php,我在其中使用正则表达式解析查询字符串并获取这些参数,如下所示:
$request_URI = $_SERVER['REQUEST_URI']; preg_match('/\/[^\/]+\/([^\/]+)\/([^\/]+)\/([^\/]+)/', $request_URI, $matches);

问题
Apache 成功地重定向了所有内容,但它还包括对所有资源的所有额外请求,因此它不是获取资源而是让我获得相同的 index.php。

目前我的文档根目录中有这个 .htaccess 文件:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]

请注意,我正在为这个项目使用虚拟主机:
<VirtualHost *:80>
ServerName masuma.hyperauto.local
DocumentRoot "c:/wamp64/www/projects/masuma landing"
</VirtualHost>
我的 index.php 在上面的这个根目录中。

我也启用了 RewriteEngine 日志,在这里发现了一些有趣的东西,但我仍然无法解决这个问题:

add path info postfix: C:/wamp64/www/projects/masuma landing/test -> C:/wamp64/www/projects/masuma landing/test/test/firm/css/bootstrap.min.css, referer: host_here/test/test/firm/toyota
strip per-dir prefix: C:/wamp64/www/projects/masuma landing/test/test/firm/css/bootstrap.min.css -> test/test/firm/css/bootstrap.min.css, referer: host_here/test/test/firm/toyota
applying pattern '.*' to uri 'test/test/firm/css/bootstrap.min.css', referer: host_here/test/test/firm/toyota
rewrite 'test/test/firm/css/bootstrap.min.css' -> 'index.php?url=test/test/firm/css/bootstrap.min.css', referer: host_here/test/test/firm/toyota
split uri=index.php?url=test/test/firm/css/bootstrap.min.css -> uri=index.php, args=url=test/test/firm/css/bootstrap.min.css, referer: host_here/test/test/firm/toyota <br><br>

我可以在这里看到,它将查询字符串中的路径添加到资源文件名中,例如 test/test/firm/css/bootstrap.min.css,但资源在文档中根!

UPD:解决方案
我的RewriteRule 中的标志[DPI] 解决了这个问题。
现在规则看起来像RewriteRule .* index.php?url=$0 [QSA,L,DPI]
而且,重要的是,我仍然能够在$_SERVER['REQUEST_URI'] 中获得原始查询路径

【问题讨论】:

  • 如果Apache成功重写到index.php,究竟是什么问题?
  • RewriteConds 检查传入的文件/文件夹路径是否不存在,然后重定向到 index.php。澄清“对所有资源的所有额外请求”。
  • 它组成不存在C:/wamp64/www/projects/masuma landing/test/test/firm/css/bootstrap.min.css
  • @Deadooshka 我希望它获取相对于文档根目录的资源,但如果请求 URI 是,如 host_name/anything/something,则链接变为 什么/什么/style.css,什么时候应该是host_name/style.css
  • 也可以考虑关闭AcceptPathInfo

标签: apache .htaccess mod-rewrite url-rewriting apache2.4


【解决方案1】:

您是否只需要 RewriteRule 上的 [DPI] 标志来消除计算的 PATH_INFO?

【讨论】:

  • 非常感谢,现在似乎一切正常,我仍然能够在 $_SERVER['REQUEST_URI'] 中获取原始查询路径
猜你喜欢
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
  • 2013-12-31
  • 2013-02-03
  • 1970-01-01
  • 2016-05-10
相关资源
最近更新 更多