简体   繁体   English

如何使用带动态参数的 .htaccess 重定向 URL?

[英]How to redirect URL using .htaccess with dynamic parameter?

I want to redirect我想重定向

https://example.com/product-info/A100001

to

https://example.com/product-info/index/index/id/A100001

using.htaccess redirect rule使用.htaccess 重定向规则

A100001 will be dynamic like A100001 将是动态的

A100001
A100002
A100003
A100004
....

I am trying this我正在尝试这个

RewriteEngine on
RewriteCond %{QUERY_STRING} product-info/A100001
RewriteRule ^$ /routing/index/index/id/? [L,R=301]

Source来源

Also tried other example but not working in my scnario还尝试了其他示例,但在我的场景中不起作用

Anyone who expert in htacees rules can help me in this.任何精通 htacees 规则的人都可以在这方面帮助我。

 RewriteCond %{QUERY_STRING} product-info/A100001 RewriteRule ^$ /routing/index/index/id/? [L,R=301]

Your example URL contains a URL-path only, it does not contain a query string .您的示例 URL 仅包含 URL 路径,不包含查询字符串 The rule you've posted would redirect /?product-info/A100001 to /routing/index/index/id/ .您发布的规则会将/?product-info/A100001/routing/index/index/id/

Try something like the following instead:请尝试以下内容:

RewriteRule ^(product-info)/(A\d{6})$ /$1/index/index/id/$2 [R=302,L]

The above would redirect a request of the form /product-info/A123456 to /product-info/index/index/id/A123456 .上面的代码会将/product-info/A123456形式的请求重定向到/product-info/index/index/id/A123456

The $1 backreference simply contains product-info , captured from the RewriteRule pattern (saves repitition) and $2 contains the dynamic part (an A followed by 6 digits). $1反向引用仅包含product-info ,从RewriteRule模式(保存重复)中捕获, $2包含动态部分( A后跟 6 位数字)。

This is a 302 (temporary) redirect.这是一个 302(临时)重定向。 Always test first with a 302 to avoid potential caching issues.始终首先使用 302 进行测试,以避免潜在的缓存问题。

The order of directives in your .htaccess file is important. .htaccess文件中指令的顺序很重要。 This rule will likely need to go near the top of the file, before any existing rewrites.在任何现有重写之前,此规则可能需要在文件顶部附近添加 go。


UPDATE:更新:

redirection is working with your code, Can you please let me know the parameter pattern, I need the number from A452218 to A572217重定向正在使用你的代码,你能告诉我参数模式吗,我需要从 A452218 到 A572217 的数字

Regex does not handle numeric ranges, only character ranges. Regex 不处理数字范围,只处理字符范围。 If you specifically only want to match numbers in the stated range then you would need to do something (more complex) like this:如果您特别只想匹配指定范围内的数字,那么您需要执行以下操作(更复杂):

RewriteRule ^(product-info)/A(45221[89]|4522[2-9]\d|452[3-9]\d{2}|45[3-9]\d{3}|4[6-9]\d{4}|5[0-6]\d{4}|57[01]\d{3}|572[01]\d{2}|57220\d|57221[0-7])$ /$1/index/index/id/A$2 [R=302,L]

NB: The $2 backreference now only contains the dynamic number, less the A prefix, which is now explicitly included in the substitution string.注意: $2反向引用现在只包含动态数字,减去A前缀,它现在明确包含在替换字符串中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM