RewriteRule 导致页面重新加载两次 [英] RewriteRule causes page to reload twice

查看:30
本文介绍了RewriteRule 导致页面重新加载两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的页面设计了两个不同的 RewriteRules:

I shaped two different RewriteRules for my page:

# Enable URL Rewriting
RewriteEngine on

# exclude followed stuff
RewriteRule ^(js|img|css|favicon\.ico|image\.php|anprobe|content|libs|flash\.php|securimage)/ - [L,QSA,S=2]

# conditions (REQUEST dont point @ file|dir|link)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-F
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

# rules
RewriteRule ^(?!index\.php)brillen/(.*(brillen)|360|neu)/(.*)([a-zA-Z0-9]{5}-[a-zA-Z0-9]{5}(?!\.))(.*)$     /index.php/brillen/$1?art_id=$4&$5&%{QUERY_STRING}      [NS,QSA,L]
RewriteRule ^(?!index\.php)(.*)$                                                            /index.php/$1                                   [NS,QSA,L]

...而且我遇到了一个奇怪的问题,它是每个请求导致页面内部加载两次,从而导致db actions和email dispatch也被执行两次的问题.

... and I'm encountering a strange problem, which lies in every request causing the page internally to load twice, which leads to the problem that db actions and email dispatching are also executed twice.

有人对此有什么想法吗?

Does anyone have an idea concerning that?

提前致谢!

注意 1:根据浏览器的资源跟踪,所有请求的资源都是有效和可用的.

Note 1: All requested resources are valid and available according to the browser's resource tracking.

注意 2: 问题可能源于对 PATH_INFO 的保留和后处理?(/index.php/$1 =>/index.php/foo/bar/...)

Note 2: May the problem originate in retaining and post-processing the PATH_INFO? (/index.php/$1 => /index.php/foo/bar/...)

推荐答案

重写引擎无法使单个 HTTP 请求运行两次.它将对 Apache 的 HTTP 请求路由到静态文件、代理函数或模块(如 PHP),并在请求中进行更改.但它不能克隆请求并给它 2 次给 apache.

The rewrite Engine cannot make a single HTTP request run twice. It routes the HTTP request for Apache to either a static file, a proxy function, or a module (like PHP) with alteration in the request. But it cannot clone the request and give it 2 times to apache.

当您遇到任何运行两次"问题时,您很可能会遇到空图片网址错误.事实上,这并不是一个真正的错误,它是 HTML 的一个特性(至少在 HTML5 之前)和一个 url 解析的特性.

When you have any "run twice" problem chances are that you are hit by the empty image url bug. In fact it's not really a bug it's a feature of HTML (at least before HTML5) and a feature of url-parsing.

如果你得到一个空的 GET url,HTML 声明浏览器应该使用相同的参数重新发送相同的查询(给他当前页面的查询).这可以使 POST 请求发生 2 次(如果请求的第一页是 POST).那么这些空的 GET url 在哪里呢?大多数时候你会得到:

If you get somewhere an empty GET url, HTML states that the browser should re-send the same query (the one that gave him the current page) with same parameters. This can make a POST request happen 2 times (if the requested 1st page were a POST). So where are these empty GET url? Most of the time you get either :

<IMG SRC="" ...> (in the HTML)

或:

url() (in the css)

或:

<script type="text/javascript" src=""></script>
<link rel="stylesheet" type="text/css" href=""> (in the HTML headers)

另请阅读@Jon 关于网站图标查询的回答.您应该始终使用 wget 或 telnet 80 查询在没有浏览器行为的情况下测试结果.

Read also @Jon answer about the favicon query. You should always test the result without browsers behaviours by using wget or telnet 80 queries.

更新:这个博客 增加了 HTML5,应该可以消除现代浏览器的这种行为.

Update: detailled explanations and followups available on this blog with HTML5 additions which should remove this behavior for modern browsers.

这篇关于RewriteRule 导致页面重新加载两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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