Rewriteengine in .htaccess to catch files not ending in html

后端 未结 3 411
情书的邮戳
情书的邮戳 2021-01-18 23:34

I\'d like to use mod rewrite in to convert web page addresses like /directory to /directory/index.html, in a standard LAMP hosting situatio

相关标签:
3条回答
  • 2021-01-19 00:19

    Use a RewriteCond directive to check whether the URL path does not end with a .html:

    RewriteCond %{REQUEST_URI} !\.html$
    RewriteRule ^(.*[^/])?/?$ $1/index.html [L]
    

    Edit   You’re using a look-ahead assertion ((?!…)). But there isn’t anything after .* (only a $). So try a look-behind assertion instead:

    RewriteRule ^.*$(?<!html) $0/index.html [L]
    

    But note that you probably need Apache 2.2 to use these assertions.

    0 讨论(0)
  • 2021-01-19 00:27

    For why adding the x makes it work: If the replacement will match the regex, the RewriteRule will be applied again. As an example, this causes an error:

    RewriteRule ^(.*)$ $1.rb
    

    because it would replace script with script.rb. That matches the regex, so it replaces script.rb with script.rb.rb, again and again...

    This is hinted at in the error log:

    Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary.
    

    In your example, you add index.html to the end. When there is an x at the end of the regex, then it won't match your replacement, which ends in an l.

    0 讨论(0)
  • 2021-01-19 00:27

    Well, for actually making it work, you could just use a negative lookbehind instead of a lookahead:

    RewriteRule ^(.*)(?<!html)$ $1/index.html [L]
    

    I'm not sure offhand why adding the 'x' makes it work, I'll edit if I figure it out.

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