Many posters have problems debugging their RewriteRule and RewriteCond statements within their .htaccess files. Most of these are using a shared hosting service and therefore don't have access to the root server configuration. They cannot avoid using .htaccess files for rewriting and cannot enable a RewriteLogLevel" as many respondents suggest. Also there are many .htaccess-specific pitfalls and constraints are aren't covered well. Setting up a local test LAMP stack involves too much of a learning curve for most.

所以我的问题是我们建议他们如何自己调试他们的规则。以下是我的一些建议。其他建议将不胜感激。

Understand that the mod_rewrite engine cycles through .htaccess files. The engine runs this loop: do execute server and vhost rewrites (in the Apache Virtual Host Config) find the lowest "Per Dir" .htaccess file on the file path with rewrites enabled if found(.htaccess) execute .htaccess rewrites (in the user's directory) while rewrite occurred So your rules will get executed repeatedly and if you change the URI path then it may end up executing other .htaccessfiles if they exist. So make sure that you terminate this loop, if necessary by adding extra RewriteCond to stop rules firing. Also delete any lower level .htaccess rewrite rulesets unless explicitly intent to use multi-level rulesets. Make sure that the syntax of each Regexp is correct by testing against a set of test patterns to make sure that is a valid syntax and does what you intend with a fully range of test URIs. See answer below for more details. Build up your rules incrementally in a test directory. You can make use of the "execute the deepest .htaccess file on the path feature" to set up a separate test directory (tree) and debug rulesets here without screwing up your main rules and stopping your site working. You have to add them one at a time because this is the only way to localise failures to individual rules. Use a dummy script stub to dump out server and environment variables. (See Listing 2)If your app uses, say, blog/index.php then you can copy this into test/blog/index.php and use it to test out your blog rules in the test subdirectory. You can also use environment variables to make sure that the rewrite engine in interpreting substitution strings correctly, e.g. RewriteRule ^(.*) - [E=TEST0:%{DOCUMENT_ROOT}/blog/html_cache/$1.html] and look for these REDIRECT_* variables in the phpinfo dump. BTW, I used this one and discovered on my site that I had to use %{ENV:DOCUMENT_ROOT_REAL} instead. In the case of redirector looping REDIRECT_REDIRECT_* variables list the previous pass. Etc.. Make sure that you don't get bitten by your browser caching incorrect 301 redirects. See answer below. My thanks to Ulrich Palha for this. The rewrite engine seems sensitive to cascaded rules within an .htaccess context, (that is where a RewriteRule results in a substitution and this falls though to further rules), as I found bugs with internal sub-requests (1), and incorrect PATH_INFO processing which can often be prevents by use of the [NS], [L] and [PT] flags.

还有什么评论或建议吗?

清单1——phpinfo

<?php phpinfo(INFO_ENVIRONMENT|INFO_VARIABLES);

当前回答

如果你打算在. htaccess中编写多行规则, 甚至不要考虑尝试那些热修复方法来调试它。

我浪费了几天时间设置多条规则,没有日志的反馈,最终只能放弃。 我在我的PC上安装了Apache,将整个站点复制到它的硬盘上,并使用日志快速整理了整个规则集。 然后我回顾了我的旧规则,这些规则一直在起作用。我发现他们并没有真正按照要求去做。定时炸弹,地址稍有不同。

重写规则中有很多陷阱,这根本不是一个直接的逻辑问题。 你可以在十分钟内启动并运行Apache,它是10MB,良好的许可证,*NIX/WIN/MAC就绪,甚至不需要安装。 此外,检查服务器的标题行,如果Apache是旧的,请从存档中获取相同版本的Apache。我的OP仍然在2.0;很多东西是不支持的。

其他回答

as pointed out by @JCastell, the online tester does a good job of testing individual redirects against an .htaccess file. However, more interesting is the api exposed which can be used to batch test a list of urls using a json object. However, to make it more useful, I have written a small bash script file which makes use of curl and jq to submit a list of urls and parse the json response into a CSV formated output with the line number and rule matched in the htaccess file along with the redirected url, making it quite handy to compare a list of urls in a spreadsheet and quickly determine which rules are not working.

设置环境变量并使用报头接收它们:

你可以用RewriteRule行创建新的环境变量,如OP所述:

RewriteRule ^(.*) - [E=TEST0:%{DOCUMENT_ROOT}/blog/html_cache/$1.html]

但是,如果不能让服务器端脚本工作,那么如何读取这个环境变量呢?一个解决方案是设置一个头文件:

Header set TEST_FOOBAR "%{REDIRECT_TEST0}e"

该值接受格式说明符,包括用于环境变量的%{NAME}e说明符(不要忘记小写e)。有时,您需要添加REDIRECT_前缀,但我还不知道什么时候添加前缀,什么时候不添加前缀。

如果你打算在. htaccess中编写多行规则, 甚至不要考虑尝试那些热修复方法来调试它。

我浪费了几天时间设置多条规则,没有日志的反馈,最终只能放弃。 我在我的PC上安装了Apache,将整个站点复制到它的硬盘上,并使用日志快速整理了整个规则集。 然后我回顾了我的旧规则,这些规则一直在起作用。我发现他们并没有真正按照要求去做。定时炸弹,地址稍有不同。

重写规则中有很多陷阱,这根本不是一个直接的逻辑问题。 你可以在十分钟内启动并运行Apache,它是10MB,良好的许可证,*NIX/WIN/MAC就绪,甚至不需要安装。 此外,检查服务器的标题行,如果Apache是旧的,请从存档中获取相同版本的Apache。我的OP仍然在2.0;很多东西是不支持的。

在线。htaccess重写测试

我在谷歌上找到了这个RegEx的帮助,它为我节省了很多时间,每次我做一个小的修改都要上传新的。htaccess文件。

来自网站:

htaccess测试仪 要测试你的htaccess重写规则,只需填写你应用规则的url,把你的htaccess的内容放在更大的输入区域,然后按“现在检查”按钮。

一个是我浪费的几个小时:

如果你已经应用了所有这些技巧,但因为你没有访问服务器错误日志而只出现了500个错误,也许问题不在.htaccess文件中,而在它重定向到的文件中。

在我修复了我的.htaccess问题之后,我又花了两个多小时试图修复它,即使我只是忘记了一些权限。