0

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 -

1 Answer 1

1

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

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

2 Comments

This doesnt seem to effect the url in anyway when i put it into my .htacess file?
This is tested and working on my Apache. Make sure this rule is placed before other rules. What is the exact URL you used for testing this?

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.