如何使用最多 4 个变量和新文本将下划线重写为破折号?
如何使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设这应该是一个外部重定向(为了“更正”URL),而不是内部重写,正如您似乎正在尝试做的那样?
请尝试这样操作:
每个“变量”仅限于字符
az
、AZ
和0-9
(尽管您在变量中省略了数字) example 指令,您的示例 URL 严格包含数字,并且您没有另外说明)。您可以更“动态”地执行此操作,以避免在每个规则中重复
text
和newtext
,但这可能会使其更加复杂,而且似乎没有必要考虑到它似乎是静态文本替换和有限数量的“变量”。这有许多问题/异常:
^
。/text_var.html
),那么生成的替换将是newtext-var---.html
(如果不是因为错误)^
) - 我确信这不是意图。/text_one_two_third_(((four).html
会匹配,但只有one
、two
、third
和 < 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:
Each "variable" is limited to the characters
a-z
,A-Z
and0-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
andnewtext
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".This has a number of issues/anomalies:
^
in the substitution string./text_var.html
) then the resulting substitution would benewtext-var---.html
(if not for the erroneous^
) - which I'm sure is not the intention./text_one_two_three_(((four).html
would be matched, but onlyone
,two
,three
andfour
are captured./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).