Apache RewriteEngine - Rewrite anything that not e

2019-09-17 22:04发布

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).

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>

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

1条回答
Summer. ? 凉城
2楼-- · 2019-09-17 22:44

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

查看更多
登录 后发表回答