Tips for debugging .htaccess rewrite rules

后端 未结 16 3109
萌比男神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: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.

提交回复
热议问题