提示调试的.htaccess重写规则 [英] Tips for debugging .htaccess rewrite rules

本文介绍了提示调试的.htaccess重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多海报都在他们的 的.htaccess 文件调试他们的重写规则和的RewriteCond报表的问题。大多数的这些正在使用共享托管服务,因此,也不能访问根服务器配置。他们无法避免使用的.htaccess 的改写和文件的不能的启用RewriteLogLevel许多受访者认为,也有许多的.htaccess 特异性缺陷和约束不包括好。建立本地测试LAMP堆栈涉及太多的学习曲线的大多数。

所以在这里我Q是如何将我们推荐他们的调试的规则的自身的。我在下面提供了一些建议。其他的建议将是AP preciated。

  1. 记者了解到,通过的.htaccess 文件 mod_rewrite的引擎周期。发动机运行这个循环:

      DO
      执行服务器和虚拟主机改写(在Apache虚拟主机配置)
      找到最低的每目录的.htaccess与重写文件路径文件中启用
      如果找到(的.htaccess)
         执行的.htaccess重写(在用户的目录)
    而重写发生
     

    所以,你的规则将得到重复执行,如果你改变了URI路径,然后它可能最终如果它们存在执行其他的.htaccess 文件。因此,请确保您终止这个循环中,如果有必要通过增加额外的的RewriteCond 停止规则射击。同时删除较低水平的.htaccess 重写规则,除非明确意图使用多层次的规则集。

  2. 确保每个正则表达式的语法是否正确通过对一组测试模式的测试,以确保这是一个有效的语法和做什么,你打算用全范围试验的URI。请参阅<一href="http://stackoverflow.com/questions/9153262/tips-for-debugging-htaccess-rewrite-rules/9261963#9261963">answer下面的更多细节。

  3. 在测试目录逐步建立你的规则。您可以使用执行最深的.htaccess 文件上路径功能设置一个单独的测试目录(树)和调试规则集这里没有搞砸了你的主要规则和停止你的网站工作。你必须添加它们一次一个,因为这是本地化失败中各个规则的唯一方法。

  4. 使用一个虚拟的脚本桩倾倒了服务器和环境变量。 (请参阅清单2 )如果你的应用程序使用,比方说,博客/ index.php文件,那么你可以复制到这一测试/blog/index.php ,并用它来测试你的博客规则,在测试子目录中。您还可以使用环境变量来确保正确的跨preting替换字符串重写引擎,如:

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

    和寻找这些的重定向_ * 中的phpinfo转储变量。顺便说一句,我用这一个,发现在我的网站,我不得不使用%{ENV:DOCUMENT_ROOT_REAL} 代替。在重定向循环的情况下的 REDIRECT_REDIRECT _ * 变量列出previous通。等等。

  5. 请确保你不被你的浏览器缓存咬不正确301重定向。请参阅<一href="http://stackoverflow.com/questions/9153262/tips-for-debugging-htaccess-rewrite-rules/9261963#9204355">answer下面。我要感谢乌尔里希Palha 这一点。

  6. 重写引擎似乎一个的.htaccess 范围内级联规则敏感,(这正是一个重写规则导致替代,这属于虽然进一步的规则),因为我发现的bug内部子请求的(1)和不正确的 PATH_INFO 处理这往往是prevents通过使用[NS],[L]和[PT]标志。

还有什么意见或建议?

清单1 - 的phpinfo

 &LT; PHP的phpinfo(INFO_ENVIRONMENT | INFO_VARIABLES)?;
 

解决方案

下面是对测试规则一些额外的提示,可缓解共同主办的

调试,为用户

1。使用假用户代理

在测试新的规则,添加一个条件,只用用户代理,你将用你的要求执行它。这样也不会影响您的网站任何人。

例如

  #protect用假用户代理
的RewriteCond%{HTTP_USER_AGENT} ^我-虚假用户代理$
#Here是我测试的实际规则
的RewriteCond%{HTTP_HOST}!^ WWW \。域\ .COM $ [NC]
重写规则^ HTTP://www.domain.com% {REQUEST_URI} [L,R = 302]
 

如果您使用的是Firefox,您可以使用用户代理切换器创建假的用户代理字符串和测试。

2。不要使用301,直到你完成测试

我已经看到了那么多帖子,人们仍在测试其规则​​和他们使用的是301的。 不要

如果您不使用建议1您的网站,不仅你的,但任何人访问您的网站的时候将受301

请记住,他们是永久性的,您的浏览器会预先缓存。 使用302,而不是直到你确信,然后将其更改为301。

3。请记住,301的正在积极缓存在你的浏览器

如果您的规则不起作用,它看起来你的权利,你没有使用建议1和2,然后重新测试清除浏览器缓存或之后,而在私人浏览。

4。使用HTTP捕捉工具

使用一个HTTP捕捉工具如提琴手,查看您的浏览器和服务器之间的实际HTTP流量。

而另一些人可能会说,你的网​​站看起来不正确,你可以改为看到并报告所有图像,CSS和JS都返回404错误,迅速缩小的问题。

虽然别人会报告你开始在URL A和结束于网址ç,您将可以看到他们开始在 URL下的,分别为302重定向到URL B和301重定向到URLç。即使URL C盘的最终目标,你就知道这是不好的搜索引擎优化,需要固定的。

您可以看到,是在服务器端,重放的要求设置缓存头,修改请求头进行测试......


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.

So my Q here is how would we recommend that they debug their rules themselves. I provide a few suggestions below. Other suggestions would be appreciated.

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

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

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

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

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

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

Any more comment or suggestions?

Listing 1 -- phpinfo

<?php phpinfo(INFO_ENVIRONMENT|INFO_VARIABLES);

解决方案

Here are a few additional tips on testing rules that may ease the debugging for users on shared hosting

1. Use a Fake-user agent

When testing a new rule, add a condition to only execute it with a fake user-agent that you will use for your requests. This way it will not affect anyone else on your site.

e.g

#protect with a fake user agent
RewriteCond %{HTTP_USER_AGENT}  ^my-fake-user-agent$
#Here is the actual rule I am testing
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] 
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [L,R=302] 

If you are using Firefox, you can use the User Agent Switcher to create the fake user agent string and test.

2. Do not use 301 until you are done testing

I have seen so many posts where people are still testing their rules and they are using 301's. DON'T.

If you are not using suggestion 1 on your site, not only you, but anyone visiting your site at the time will be affected by the 301.

Remember that they are permanent, and aggressively cached by your browser. Use a 302 instead till you are sure, then change it to a 301.

3. Remember that 301's are aggressively cached in your browser

If your rule does not work and it looks right to you, and you were not using suggestions 1 and 2, then re-test after clearing your browser cache or while in private browsing.

4. Use a HTTP Capture tool

Use a HTTP capture tool like Fiddler to see the actual HTTP traffic between your browser and the server.

While others might say that your site does not look right, you could instead see and report that all of the images, css and js are returning 404 errors, quickly narrowing down the problem.

While others will report that you started at URL A and ended at URL C, you will be able to see that they started at URL A, were 302 redirected to URL B and 301 redirected to URL C. Even if URL C was the ultimate goal, you will know that this is bad for SEO and needs to be fixed.

You will be able to see cache headers that were set on the server side, replay requests, modify request headers to test ....


这篇关于提示调试的.htaccess重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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