Apache RewriteEngine - 将不存在的任何内容重写为 index.php
Apache RewriteEngine - Rewrite anything that not exists to index.php
我现在真的很挣扎。
首先,我在 Google 和此处进行了搜索,none 找到了对我有用的解决方案。我现在真的没有太多时间来完全深入研究 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=[=17=] [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,但资源是在文档根目录中!
更新:解决方案
我的 RewriteRule
中的标记 [DPI]
解决了问题。
现在规则看起来像 RewriteRule .* index.php?url=[=24=] [QSA,L,DPI]
而且,重要的是,我仍然能够在 $_SERVER['REQUEST_URI']
中获得原始查询路径
您是否只需要 RewriteRule 上的 [DPI]
标志来摆脱计算的 PATH_INFO?
我现在真的很挣扎。
首先,我在 Google 和此处进行了搜索,none 找到了对我有用的解决方案。我现在真的没有太多时间来完全深入研究 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=[=17=] [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,但资源是在文档根目录中!
更新:解决方案
我的 RewriteRule
中的标记 [DPI]
解决了问题。
现在规则看起来像 RewriteRule .* index.php?url=[=24=] [QSA,L,DPI]
而且,重要的是,我仍然能够在 $_SERVER['REQUEST_URI']
您是否只需要 RewriteRule 上的 [DPI]
标志来摆脱计算的 PATH_INFO?