干净 URL 的正则表达式

发布于 2024-09-13 10:34:12 字数 518 浏览 0 评论 0原文

干净的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烦人精 2024-09-20 10:34:12

试试这个:

(?:^|/)[^./]+(?:\.html)?$

翻译:如果有一个(如果没有,则从字符串的开头)匹配除 /之外的任何内容中的一个或多个,则从最后一个 / 开始>.,可选地以 .html 终止。

Try this:

(?:^|/)[^./]+(?:\.html)?$

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.

李不 2024-09-20 10:34:12
/\.html$|(?:^|.*\/)[^\.]+$/

以“.html”结尾或没有“.”从 url 的开头或最后一个 / 到结尾(您可以拥有包含“.”的文件夹)

/\.html$|(?:^|.*\/)[^\.]+$/

ending with ".html" or have no "." from the beginning of the url or the last / to the end (you can have folders containing a ".")

时光是把杀猪刀 2024-09-20 10:34:12

这又如何呢?

(^[^\.]+?$)|(^.+?\.html$)

它匹配不包含任何 . 的字符串或以 .html 结尾的字符串。

或者,如果您想在“文件夹”名称中使用点,请使用此选项:

(^.+?/[^\.]+?$)|(^.+?\.html$)

匹配最后一个 / 之后不包含 . 的字符串或以 < 结尾的字符串代码>.html。

What about this?

(^[^\.]+?$)|(^.+?\.html$)

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:

(^.+?/[^\.]+?$)|(^.+?\.html$)

Matches either a string that contains no . after the last / or a string that ends with .html.

魂ガ小子 2024-09-20 10:34:12

用于带有 .html 扩展选项的干净 URL 的正则表达式选项:

^/([\w\/]*(\.html)?)$

完整的 lighttpd.conf 行:

url.rewrite = ( "^/([\w\/]*(\.html)?)$" => "index.php?page=$1" )

快速提醒:文件的绝对路径或基本 href 应该在传递此正则表达式的任何页面中实现。

Regex option for clean URLs with .html extension option:

^/([\w\/]*(\.html)?)$

Full lighttpd.conf line:

url.rewrite = ( "^/([\w\/]*(\.html)?)$" => "index.php?page=$1" )

Quick reminder: Absolute paths to files or a base href should be implemented in any pages that pass this regex.

说好的呢 2024-09-20 10:34:12

不要尝试使用正则表达式来匹配您想要允许的 URL(正如其他答案似乎尝试的那样),而是使用正则表达式来匹配您想要阻止的 URL:

\.(?!html$)[^./]*$

此正则表达式匹配 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:

\.(?!html$)[^./]*$

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文