干净 URL 的正则表达式
干净的 URL 看起来很简单,但我需要一个特殊的情况。我希望能够包含 .html 后缀或不包含任何内容,但不包含任何其他扩展名:
someurl = pass someurl/somepage = pass someurl.html = pass someurl/somepage.html = pass someurl.css = fail someurl.exe = fail someurl.anyotherextension = fail someurl/someother.ext = fail
这可能吗?我是否必须以某种方式排除我不想要的扩展?
编辑:
到目前为止,所有答案似乎都不起作用。我自己发现的唯一有效的方法是:
^/([\w]*(.html)?)$but it will not work with slashes in the url. Adding a slash inside the character class brackets makes it fail.
Clean URLs seem pretty simple but I'm in need of a special situation. I would like to be able to include the .html suffix or nothing at all but not any other extension:
someurl = pass someurl/somepage = pass someurl.html = pass someurl/somepage.html = pass someurl.css = fail someurl.exe = fail someurl.anyotherextension = fail someurl/someother.ext = fail
Is this possible? Would I have to somehow exclude the extensions I don't want?
Edit:
None of the answers so far seem to work. The only thing that I've discovered on my own that works is:
^/([\w]*(.html)?)$
but it will not work with slashes in the url. Adding a slash inside the character class brackets makes it fail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
翻译:如果有一个(如果没有,则从字符串的开头)匹配除
/
或之外的任何内容中的一个或多个,则从最后一个
,可选地以/
开始>..html
终止。Try this:
Translation: starting from the last
/
if there is one (or from the beginning of the string if not) match one or more of anything except/
or.
, optionally terminated by.html
.以“.html”结尾或没有“.”从 url 的开头或最后一个 / 到结尾(您可以拥有包含“.”的文件夹)
ending with ".html" or have no "." from the beginning of the url or the last / to the end (you can have folders containing a ".")
这又如何呢?
它匹配不包含任何
.
的字符串或以.html
结尾的字符串。或者,如果您想在“文件夹”名称中使用点,请使用此选项:
匹配最后一个
/
之后不包含.
的字符串或以 < 结尾的字符串代码>.html。What about this?
This matches either a string that doesn't contain any
.
or a string that ends with.html
.Or use this, if you want to use dots in your "folder" names:
Matches either a string that contains no
.
after the last/
or a string that ends with.html
.用于带有 .html 扩展选项的干净 URL 的正则表达式选项:
完整的 lighttpd.conf 行:
快速提醒:文件的绝对路径或基本 href 应该在传递此正则表达式的任何页面中实现。
Regex option for clean URLs with .html extension option:
Full lighttpd.conf line:
Quick reminder: Absolute paths to files or a base href should be implemented in any pages that pass this regex.
不要尝试使用正则表达式来匹配您想要允许的 URL(正如其他答案似乎尝试的那样),而是使用正则表达式来匹配您想要阻止的 URL:
此正则表达式匹配 URL 的扩展名,除非扩展名是
.html
。没有扩展名或.html
扩展名的 URL 不匹配。您的示例不包含带有查询 (?param=value
) 或片段 (#anchor
) 的 URL,因此正则表达式不会考虑这些。我还假设您的正则表达式风格支持 lookahead。Instead of trying to use a regex to match the URLs you want to allow (as the other answers seem to try), use a regex to match the URLs that you want to block:
This regex matches the extension of a URL, unless the extension is
.html
. URLs without an extension or an.html
extension are not matched. Your examples don't include URLs with queries (?param=value
) or fragements (#anchor
) so the regex does not account for those. I'm also assuming your regex flavor supports lookahead.