.htaccess advanced rewrite rule for numbers(digits)

Viewed 24

I have an URL: example.com/fruits12345/mango

I want to redirect it to example.com/fruits/12345/mango

*any number of digits after fruits should be redirected with fruits/NUMBERS Tried But failed as it creates too many redirects:

RewriteEngine on 
RewriteBase / 
RewriteRule ^fruits(.*)/(.*) /fruits/$1/$2 [R=301,L]
1 Answers

You may use this rule:

RewriteEngine on 

RewriteRule ^(fruits)(\d+)/(.*) /$1/$2/$3 [R=301,L,NC,NE]

Make sure to clear your browser cache before testing this rule.


Problem with your rule is that you are matching .* after fruits which matches anything so a redirected URL /fruits/12345/mango also matches this pattern and gets redirected again and again causing redirect loop.

Related