.htaccess RewriteRule 帮助

发布于 2024-09-03 14:09:11 字数 225 浏览 1 评论 0 原文

以下网址的重写规则是什么

mysite.com/001234-some-keywords.html

我想捕获 6 位参考号,例如 ([0-9]{6})
我的索引文件是

templates/default/index.php?ref=001234

有一天我做了一个正则表达式速成课程..我保证。

What is the rewrite rule for following URL

mysite.com/001234-some-keywords.html

I want to capture the 6 digit reference number something like ([0-9]{6})
my indexed file is

templates/default/index.php?ref=001234

One day I do a crashcourse regex.. I promise.

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

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

发布评论

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

评论(1

南街女流氓 2024-09-10 14:09:11

可能

RewriteRule ^([0-9]{6}).*$ templates/default/index.php?ref=$1
  • ([0-9]{6}) 已经是正确的,它捕获六位数字
  • .* 匹配之后的任何字符。
  • $1 被第一个捕获组 () 的内容替换

如果您想限制 URL 仅在恰好有六位数字时匹配,那么它应该如下所示(注意破折号 -):

RewriteRule ^([0-9]{6})-.*$ templates/default/index.php?ref=$1

或仅在以 .html 结尾时匹配 URL:

RewriteRule ^([0-9]{6})-.*?\.html$ templates/default/index.php?ref=$1

我建议阅读 mod_rewrite 文档
regular-expressions.info 也非常适合学习正则表达式。

Probably

RewriteRule ^([0-9]{6}).*$ templates/default/index.php?ref=$1
  • ([0-9]{6}) was already correct, it captures the six digits
  • .* matches any character afterwards.
  • $1 gets replaced by the contents of the first capture group ()

If you want to restrict the URL to match only if there are exactly six digits, then it should look like this (note the dash -):

RewriteRule ^([0-9]{6})-.*$ templates/default/index.php?ref=$1

or matching the URL only if it is ending in .html:

RewriteRule ^([0-9]{6})-.*?\.html$ templates/default/index.php?ref=$1

I suggest to read the mod_rewrite documentation.
Also regular-expressions.info is great for learning regular expressions.

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