如何调试mod_rewrite规则? [英] How can I debug mod_rewrite rules?

查看:278
本文介绍了如何调试mod_rewrite规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问一个问题,回答我自己的情况下,这我张贴任何人的利益,谁拥有了同样的问题。

This is a case of "ask a question and answer it myself", which I'm posting for the benefit of anyone who has the same problem.

我遇到了一些问题,一个共享的服务器上,我甚至不能访问Apache的错误日志上调试的.htaccess文件一个mod_rewrite的规则集。我发现了一个巧妙的方法来进行调试,这是这样的:

I had some problems debugging a mod_rewrite ruleset in a .htaccess file on a shared server, where I couldn't even access the Apache error logs. I found a neat way to debug them, which is this:

  1. 编写一个简单的打印出它的查询字符串变量很短的脚本。例如在PHP中:

  1. Write a short script that simply prints out it's querystring variables. e.g. in PHP:

<?='<pre>',htmlentities(print_r($_GET,true)),'</pre>'?>

是你所需要的。

is all you need.

比方说,你的名字这个脚本show.php,并把它放在/的public_html。然后,在你的.htaccess文件,找出你认为可能导致该问题的规则集中的点,然后插入这条规则:

Let's say you name this script "show.php" and put it in /public_html. Then in your .htaccess file, identify the point in your ruleset that you think might be causing the problem, and insert this rule:

RewriteRule (.*) /show.php?url=$1 [END]

的效果是一样的在常规程序中插入一个PRINT语句。它会告诉你的是:(a)你到达的规则集这一点上,和(b)当前重写URL是什么。

The effect is the same as inserting a PRINT statement in a regular program. It'll let you know that (a) You reached that point in the ruleset, and (b) what the current rewritten URL is.

这不是闪存作为一个真正的调试工具,但它能够完成任务。

It's not as flash as a real debugging tool, but it gets the job done.

如果您使用的是Apache&LT; 2.3.9,你将不得不使用 [L] ,而不是的[结束] 。在这种情况下,什么看出来的是,你的规则集不应该试图改写/show.php到别的。如果这是一个问题,你可以通过添加这条规则在最高层修复:

If you're using Apache <2.3.9, you'll have to use [L] instead of [END]. In that case, something to look out for is that your ruleset should not attempt to rewrite "/show.php" to anything else. If that's a problem, you can fix it by adding this rule at the very top:

RewriteRule ^show.php$ - [L]

...只记得删除它,当你完成调试!

...Just remember to remove it when you're done debugging!

推荐答案

非常有益的见解。多年来,我一直在试图找出如何,而不需要有root权限,不得不把规则在httpd.conf调试mod_rewrite规则。这就是了!

Very helpful insight. For years I've been trying to figure out how to debug mod_rewrite rules without needing to have root access and having to put the rules in httpd.conf. This does it!

您的PHP一个小错误:

You have one minor mistake in your PHP:

<?='<pre>',htmlentities(print_r($_GET),true),'</pre>'?>

在此code,的print_r()输出一切都在$ _GET到标准输出,然后返回值为true,这ヶ辆()拿起作为第一个参数。ヶ辆()还接收字面真正作为第二个参数,这是一个可选参数,告诉ヶ辆()是否要惹单和/或双引号。

In this code, print_r() outputs everything in $_GET to stdout and then returns the value true, which htmlentities() picks up as its first argument. htmlentities() also receives the literal true as its second argument, which is an optional argument that tells htmlentities() whether or not to mess with single- and/or double-quotes.

我想你打算什么:

<?='<pre>',htmlentities(print_r($_GET, true)),'</pre>'?>

本通知的print_r()格式化一切都在$ _GET。传递true作为第二个参数的print_r()告诉它不要将结果输出到标准输出,而是把结果在一个字符串,返回字符串的print_r()的返回值。ヶ辆()然后接收该字符串作为其一个输入参数,并执行适当的替换强制浏览器为是,而不是允许浏览器间$ P $角字符串来显示字符串。例如。 -

This tells print_r() to format everything in $_GET. Passing true as the second argument to print_r() tells it not to output the result to stdout, but instead to put the result in a string and return that string as print_r()'s return value. htmlentities() then receives that string as its one input parameter, and does appropriate substitutions to force the browser to display the string as is rather than allowing the browser to interpret the string. E.G. -

<i>text</i>

将得到翻译为:

would get translated to:

&lt;i&gt;text&lt;/i&gt;

这将导致浏览器显示:

which will cause the browser to display:

<i>text</i>

而不是用斜体字显示的文字文本

instead of displaying the word "text" in italics:

文本

这篇关于如何调试mod_rewrite规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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