Apache RewriteEngine-将所有不存在的内容重写为index.php [英] Apache RewriteEngine - Rewrite anything that not exists to index.php

查看:106
本文介绍了Apache RewriteEngine-将所有不存在的内容重写为index.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在真的很挣扎.
首先,我在Google和此处进行了搜索,但没有找到适合我的解决方案.我现在真的没有太多时间完全深入研究Apache文档,以对RewriteEngine有扎实的了解(以后会确定).

I'm really struggling now.
First of all, i searched on Google and here, and none of the solutions found worked for me. I don't really have much time right now to completely dig into Apache docs for solid understanding of the RewriteEngine(will do later for sure).

我需要的
我需要处理来自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日志并在此处找到了有趣的东西,但是我仍然无法解决此问题:

WHAT I NEED
I need to handle special requests from our Google Adwords campaign, which look like this: http://website.com/something/%first_param%/%second_param%/%third_param% In my document root directory, i have index.php where i parse the query string using regex and acquire these parameters, like this:
$request_URI = $_SERVER['REQUEST_URI']; preg_match('/\/[^\/]+\/([^\/]+)\/([^\/]+)\/([^\/]+)/', $request_URI, $matches);

THE PROBLEM
Apache successfully redirects everything, but it also includes all additional requests for all the resources, so instead of fetching resources it just gets me the same index.php.

Currently i have this .htaccess file in my document root:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]

Please note, that i am using Virtual Host for this project:
<VirtualHost *:80>
ServerName masuma.hyperauto.local
DocumentRoot "c:/wamp64/www/projects/masuma landing"
</VirtualHost>
I have my index.php in this root directory above.

I have also enabled RewriteEngine logs and found interesting things here, but i still can't solve this:

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']

I can see here, that it prepends the path in query string to the resource filename, like test/test/firm/css/bootstrap.min.css, but the resources are in the document root!

UPD: SOLUTION
The flag [DPI] in my RewriteRule solved the problem.
Now the rule looks like RewriteRule .* index.php?url=$0 [QSA,L,DPI]
And, what is important, i am still able to get the original query path in $_SERVER['REQUEST_URI']

推荐答案

您是否只需要在RewriteRule上使用[DPI]标志来摆脱计算出的PATH_INFO?

Do you just need the [DPI] flag on your RewriteRule to get rid of the calculated PATH_INFO?

这篇关于Apache RewriteEngine-将所有不存在的内容重写为index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆