Tips for debugging .htaccess rewrite rules

后端 未结 16 3147
萌比男神i
萌比男神i 2020-11-21 05:17

Many posters have problems debugging their RewriteRule and RewriteCond statements within their .htaccess files. Most of these are using a shar

相关标签:
16条回答
  • 2020-11-21 05:42

    Don't forget that in .htaccess files it is a relative URL that is matched.

    In a .htaccess file the following RewriteRule will never match:

    RewriteRule ^/(.*)     /something/$s
    
    0 讨论(0)
  • 2020-11-21 05:45

    If you are working with url, You might want to check if you "Enable Mod Rewrite"

    0 讨论(0)
  • 2020-11-21 05:47

    Some mistakes I observed happens when writing .htaccess

    Using of ^(.*)$ repetitively in multiple rules, using ^(.*)$ causes other rules to be impotent in most cases, because it matches all of the url in single hit.

    So, if we are using rule for this url sapmle/url it will also consume this url sapmle/url/string.


    [L] flag should be used to ensure our rule has done processing.


    Should know about:

    Difference in %n and $n

    %n is matched during %{RewriteCond} part and $n is matches on %{RewriteRule} part.

    Working of RewriteBase

    The RewriteBase directive specifies the URL prefix to be used for per-directory (htaccess) RewriteRule directives that substitute a relative path.

    This directive is required when you use a relative path in a substitution in per-directory (htaccess) context unless any of the following conditions are true:

    The original request, and the substitution, are underneath the DocumentRoot (as opposed to reachable by other means, such as Alias). The filesystem path to the directory containing the RewriteRule, suffixed by the relative substitution is also valid as a URL path on the server (this is rare). In Apache HTTP Server 2.4.16 and later, this directive may be omitted when the request is mapped via Alias or mod_userdir.

    0 讨论(0)
  • 2020-11-21 05:49

    I found this question while trying to debug my mod_rewrite issues, and it definitely has some helpful advice. But in the end the most important thing is to make sure you have your regex syntax correct. Due to problems with my own RE syntax, installing the regexpCheck.php script was not a viable option.

    But since Apache uses Perl-Compatible Regular Expressions (PCRE)s, any tool which helps writing PCREs should help. I've used RegexPlanet's tool with Java and Javascript REs in the past, and was happy to find that they support Perl as well.

    Just type in your regular expression and one or more example URLs, and it will tell you if the regex matches (a "1" in the "~=" column) and if applicable, any matching groups (the numbers in the "split" column will correspond to the numbers Apache expects, e.g. $1, $2 etc.) for each URL. They claim PCRE support is "in beta", but it was just what I needed to solve my syntax problems.

    http://www.regexplanet.com/advanced/perl/index.html

    I'd have simply added a comment to an existing answer but my reputation isn't yet at that level. Hope this helps someone.

    0 讨论(0)
  • 2020-11-21 05:50

    If you're creating redirections, test with curl to avoid browser caching issues. Use -I to fetch http headers only. Use -L to follow all redirections.

    0 讨论(0)
  • 2020-11-21 05:52

    One from a couple of hours that I wasted:

    If you've applied all these tips and are only going on 500 errors because you don't have access to the server error log, maybe the problem isn't in the .htaccess but in the files it redirects to.

    After I had fixed my .htaccess-problem I spent two more hours trying to fix it some more, even though I simply had forgotten about some permissions.

    0 讨论(0)
提交回复
热议问题