Jamezn
Jamezn

Reputation: 1

.htaccess regex redirect and replace

Going to show my lack of regex knowledge here but seem to be having trouble with the following:

I need to redirect the following example structure of a url:

www.example.com/blog-title-here/tags/this+is+a+long+tag+name

www.example.com/tag/this-is-a-long-tag-name

I need to redirect to just /tag and replace any instance of + with a -

Upvotes: 0

Views: 140

Answers (1)

anubhava
anubhava

Reputation: 784898

You can use this recursive redirect rule as your very first rule in site root .htaccess:

RewriteEngine On

RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*[ +].*)$" $1-$2 [N,NC,DPI]
RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*)$" /$1-$2 [L,R=301,NC,NE]

This will redirect http://localhost/blog-title-here/tag/this+is+a+long+tag+name to http://localhost/tag/this-is-a-long-tag-name

Upvotes: 1

Related Questions