0

I want to redirect

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

to

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

using htaccess redirect rule

A100001 will be dynamic like

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.

3
  • There are literally thousands of existing answers here on SO demonstrating how to implement a RewriteRule. Also there are many tutorials out there on the internet explaining exactly that. And there is an excellent documentation. Why don't you start yourself and try to solve that task. Then , when you fail, then is the time to ask here including your own implementation attempt along with an explanation what exactly the issue is. Commented Oct 7, 2022 at 13:34
  • Thanks for your advice, Yes I am trying at my end but rules are not working as expected, that's why I raising the issue here to get help. Commented Oct 7, 2022 at 13:47
  • Which is perfectly fine. But then, just as written above, please include your best own implementation attempt and tell us what exactly is not working as expected. Commented Oct 7, 2022 at 13:58

1 Answer 1

1
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. The rule you've posted would redirect /?product-info/A100001 to /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.

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).

This is a 302 (temporary) redirect. Always test first with a 302 to avoid potential caching issues.

The order of directives in your .htaccess file is important. This rule will likely need to go near the top of the file, before any existing rewrites.


UPDATE:

redirection is working with your code, Can you please let me know the parameter pattern, I need the number from A452218 to A572217

Regex does not handle numeric ranges, only character ranges. 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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, @MrWhite, redirection is working with your code, Can you please let me know the parameter pattern, I need the number from A452218 to A572217
@PurushotamSharma The existing regex A\d{6} will cover that number range. Or do you specifically need to limit this only to numbers in that range?
If any number not this of between A452218 to A572217, I want to show an Invalid Number message
@PurushotamSharma I've updated my answer to match only numbers in the stated range.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.