需要正则表达式帮助将 URL 重写查询字符串为友好的 URL

发布于 2024-12-27 20:30:27 字数 505 浏览 0 评论 0原文

我更新了网站 CMS,URL 格式已更改。以前我有 URL /blog.aspx?Year=XXXX&Month=YY 我现在有 /blog/XXXX/YY

有人可以帮我为此创建一个正则表达式?

两个额外的注意事项:

  • 它还必须支持简单的年份 (/blog.aspx?Year=XXX),
  • 旧的月份 url 仅使用 1 位数表示个位数月份 (/blog.aspx? Year=2009&Month=2 而不是 Month=02

这是我想到的:

/blog.aspx[?]Year=([0-9]{4})([&]?)(Month=)?([0-9]*)

我似乎无法让它工作,因为我仍然收到 404页面当我访问上述网址之一时。

I updated my website CMS and the URL formats have changed. Where previously I had the URL /blog.aspx?Year=XXXX&Month=YY I now have /blog/XXXX/YY

Can someone help me create a regex for this?

Two additional notes:

  • it has to also support simply the year (/blog.aspx?Year=XXX)
  • the old Month urls use only 1 digit for single digit months (/blog.aspx?Year=2009&Month=2 instead of Month=02)

Here is what I came up with:

/blog.aspx[?]Year=([0-9]{4})([&]?)(Month=)?([0-9]*)

I can't seem to get it to work, as I still get a 404 on the page when I go to one of the above URLs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

各自安好 2025-01-03 20:30:27

这可行吗?

/blog.aspx\?Year=([0-9]{4})(?>\&?Month=?([0-9]{1,2})|)

与这些输入一起使用时,

/blog.aspx?Year=1983&Month=2
/blog.aspx?Year=1983
/blog.aspx?Year=1983&Month=12

有这个 (?>>blabla|moomoo) 语法。
如果找不到 blabla 匹配,它将匹配 moomoo

虽然我怀疑这里的正则表达式不是根本问题,但什么 CMS 处理重定向?

Is this workable?

/blog.aspx\?Year=([0-9]{4})(?>\&?Month=?([0-9]{1,2})|)

works with these input

/blog.aspx?Year=1983&Month=2
/blog.aspx?Year=1983
/blog.aspx?Year=1983&Month=12

there is this (?>blabla|moomoo) syntax.
If it cant find blabla match, it will match moomoo

Though i suspect regex here is not the root problem, what CMS handles the redirect?

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