RewriteEngine叙述在.htaccess赶上文件没有不散的HTML [英] Rewriteengine in .htaccess to catch files not ending in html
问题描述
我想使用mod-rewrite的转换网页地址例如 /目录为 /directory/index.html ,在一个标准的LAMP托管情况。我有什么作品,结尾的斜线地址。我无法找到一个方法来处理地址不结束斜线
I'd like to use mod rewrite in to convert web page addresses like /directory to /directory/index.html, in a standard LAMP hosting situation. What I have works for addresses that end in a slash. I can't find a way to handle addresses that don't end a slash.
这似乎像它应该是:
rewriterule ^(.*)/$ $1/index.html [L] /* addresses ending in / */
rewriterule ^(.*(?!html))$ $1/index.html [L] /* where the problem is */
但第二行导致500服务器错误。如果I X加一个字母到第二行:
But the second line causes a 500 server error. If I add a single letter x to the second line:
rewriterule ^(.*)/$ $1/index.html [L]
rewriterule ^(.*x(?!html))$ $1/index.html [L]
它开始工作,但最终在一个x 只为目录名。我已经尝试了很多不同的东西代替的X。什么比真实人物更复杂(如[^ x]或+)给出了500服务器错误。
It starts to work, but only for directory names that end in an x. I have tried replacing the x with many different things. Anything more complicated than real characters (like [^x] or .+) gives a 500 server error.
和,为了满足自己的好奇心,没有人知道为什么加一个真正的信令服务器错误和完美的运作规则之间的区别是什么?
And, to satisfy my own curiosity, does anyone know why the addition of a single real letter makes the difference between a server error and a perfectly functioning rule?
[公认的答案] 感谢浓汤我能近似使用的RewriteCond的解决方案:
[Accepted Answer] Thanks to Gumbo I was able to approximate a solution using rewritecond:
rewritecond %{REQUEST_URI} !\.[^/]+$
rewriterule (.+) $1/index.html [L]
这工作,但过滤器不仅仅是html的 - 它可以阻止其他页面。不幸的是,
This works, but filters more than just .html -- it could block other pages. Unfortunately,
rewritecond %{REQUEST_URI} !\.html$
导致服务器错误:
results in a server error:
请求超过了10内部重定向的限制,由于可能的配置错误。使用'LimitInternalRecursion'如果有必要提高限制
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary.
我还是想知道为什么:
rewriterule ^(.*(?!html))$ $1/index.html [L]
结果在一个循环。上半年应该检查是否的不的结束。html的。自去年下半年以来的添加的html的,这似乎是等效的功能:
results in a loop. The first half is supposed to check if it doesn't end in .html. Since the second half adds .html, it seems like the functional equivalent of:
while(substr($address,-4)!='html') $address.='html'
很显然,我失去了一些东西。
Obviously I'm missing something.
推荐答案
使用的RewriteCond
指令,以检查是否URL路径不与的.html
:
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]
但请注意,你可能需要Apache 2.2使用这些说法。
But note that you probably need Apache 2.2 to use these assertions.
这篇关于RewriteEngine叙述在.htaccess赶上文件没有不散的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!