如何使用最多 4 个变量和新文本将下划线重写为破折号?

发布于 2025-01-16 01:23:03 字数 431 浏览 3 评论 0原文

如何使用 1 到 4 个变量和新文本将下划线修改为破折号?

要从此更改:

https://example.com/Text_var1_var2_var3_var4.html 

改为:

https://example.com/NewText-var1-var2-var3-var4.html

我已经尝试过此操作,但出现 404 错误:

RewriteRule ^text_([a-zA-Z]+)_?([a-zA-Z]*)_?([a-zA-Z]*)_?\(*([a-zA-Z]*)\)*.html?$ newtext-$1-$2-$3-$4^.html

How do I Mod-Rewrite underscores to dashes with 1 to 4 variables and new text?

To change from this:

https://example.com/Text_var1_var2_var3_var4.html 

To this:

https://example.com/NewText-var1-var2-var3-var4.html

I've tried this but it gives a 404 error:

RewriteRule ^text_([a-zA-Z]+)_?([a-zA-Z]*)_?([a-zA-Z]*)_?\(*([a-zA-Z]*)\)*.html?$ newtext-$1-$2-$3-$4^.html

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

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

发布评论

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

评论(1

梦忆晨望 2025-01-23 01:23:03

我假设这应该是一个外部重定向(为了“更正”URL),而不是内部重写,正如您似乎正在尝试做的那样?

请尝试这样操作:

RewriteEngine On

# Handle from 1 to 4 "variables" and "newtext"
RewriteRule ^text_([a-zA-Z0-9]+)\.html$ /newtext-$1.html [R=302,L]
RewriteRule ^text_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)\.html$ /newtext-$1-$2.html [R=302,L]
RewriteRule ^text_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)\.html$ /newtext-$1-$2-$3.html [R=302,L]
RewriteRule ^text_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)\.html$ /newtext-$1-$2-$3-$4.html [R=302,L]

每个“变量”仅限于字符 azAZ0-9(尽管您在变量中省略了数字) example 指令,您的示例 URL 严格包含数字,并且您没有另外说明)。

您可以更“动态”地执行此操作,以避免在每个规则中重复 textnewtext,但这可能会使其更加复杂,而且似乎没有必要考虑到它似乎是静态文本替换和有限数量的“变量”。


RewriteRule ^text_([a-zA-Z]+)_?([a-zA-Z]*)_?([a-zA-Z]*)_?\(*([ a-zA-Z]*)\)*.html?$ newtext-$1-$2-$3-$4^.html

这有许多问题/异常:

  • 替换字符串中存在错误的^
  • 您正尝试在单个指令中处理所有 1 到 4 个变量。这里的问题是替换字符串中连字符的数量是“固定的”。因此,如果只有一个变量(例如 /text_var.html),那么生成的替换将是 newtext-var---.html (如果不是因为错误) ^) - 我确信这不是意图。
  • 您允许在第四个“变量”周围使用任意数量的可选括号,但您在要求中没有提及这一点?例如。 /text_one_two_third_(((four).html 会匹配,但只有 onetwothird 和 < code>four 被捕获,
  • 这是内部重写,而不是外部重定向,因此除非 /newtext-var1-var2-var3-var4.html 映射到物理文件,否则这需要。进一步重写才能正确解决(即不太可能工作)。

I'm assuming this should be an external redirect (in order to "correct" the URL), not an internal rewrite, as you appear to be trying to do?

Try it like this instead:

RewriteEngine On

# Handle from 1 to 4 "variables" and "newtext"
RewriteRule ^text_([a-zA-Z0-9]+)\.html$ /newtext-$1.html [R=302,L]
RewriteRule ^text_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)\.html$ /newtext-$1-$2.html [R=302,L]
RewriteRule ^text_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)\.html$ /newtext-$1-$2-$3.html [R=302,L]
RewriteRule ^text_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)\.html$ /newtext-$1-$2-$3-$4.html [R=302,L]

Each "variable" is limited to the characters a-z, A-Z and 0-9 (although you omitted digits in your example directive, your example URL does strictly contain digits and you've not stated otherwise).

You can do it more "dynamically" to avoid repeating text and newtext in each rule, but that would make it arguably more complex and there doesn't seem to be a need considering it would appear to be a static text replacement and a limited number of "variables".


RewriteRule ^text_([a-zA-Z]+)_?([a-zA-Z]*)_?([a-zA-Z]*)_?\(*([a-zA-Z]*)\)*.html?$ newtext-$1-$2-$3-$4^.html

This has a number of issues/anomalies:

  • You have an erroneous ^ in the substitution string.
  • You are trying to handle all 1 to 4 variables in a single directive. The problem here is that the number of hyphens in the substitution string is "fixed". So, if there was just one variable (eg. /text_var.html) then the resulting substitution would be newtext-var---.html (if not for the erroneous ^) - which I'm sure is not the intention.
  • You are allowing any number of optional parentheses surrounding the 4th "variable", but you make no mention of this in your requirements? eg. /text_one_two_three_(((four).html would be matched, but only one, two, three and four are captured.
  • This is an internal rewrite, not an external redirect. So unless /newtext-var1-var2-var3-var4.html maps to a physical file then this requires further rewriting to be resolved correctly (which is unlikely to work).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文