Forward one path to another AND convert spaces to hyphens along the way

Viewed 66

Trying to change https://www.example.com/search/label/my%20search%20term to https://www.example.com/tag/my-search-term

Per the answer from @anubhava below, Option 2 works:

RewriteEngine On
RewriteRule ^search/label/(\S+)[-\s]+(\S+)$ /tag/$1-$2 [NE,NC,L,R=301]
RewriteRule ^search/label/(\S+)$ /tag/$1 [NE,NC,L,R=301]
RewriteRule ^(search/label/\S+)\s+(\S+\s.*)$ /$1-$2 [NC,L]

Thanks!

2 Answers

You may try these recursive rules in your site root .htaccess:

Option 1:

RewriteEngine On

RewriteRule ^(search/label/\S+)\s+(\S+\s.*)$ /$1-$2 [N,NC,DPI]

RewriteRule ^search/label/(\S+)[-\s]+(\S+)$ /tag/$1-$2 [NE,NC,L,R=301]

RewriteRule ^search/label/(\S+)$ /tag/$1 [NE,NC,L,R=301]

Option 2:

RewriteEngine On

RewriteRule ^search/label/(\S+)[-\s]+(\S+)$ /tag/$1-$2 [NE,NC,L,R=301]

RewriteRule ^search/label/(\S+)$ /tag/$1 [NE,NC,L,R=301]

RewriteRule ^(search/label/\S+)\s+(\S+\s.*)$ /$1-$2 [NC,L]

Could you please try following, written as per shown samples. Please make sure you clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteRule ^search/label/([^\s]*)\s([^\s]*)\s(.*)/?$ tag/$1-$2-$3 [NE,R=301,L]


OR using \S here: Please make sure either you put above rules OR following rules only one at a time.

RewriteEngine ON
RewriteRule ^search/label/(\S*)\s(\S*)\s(.*)/?$ tag/$1-$2-$3 [NE,R=301,L]
Related