0

I'm in the process of migrating a website from an old CMS to Wordpress. The way that some of the old URLs were:

http://www.example.com/tags/tagged/tag/tag+name

For Wordpress, I need it to be the following URL:

http://www.example.com/tag/tag-name

So I need to remove the /tags/tagged sub-directories and also change the + signs to - signs. Is there any way to do this with .htaccess?

  • How generic do you need this to be? Are /tags/tagged any two subdirectories? Or some specific subdirectories or a singular specific URL-path? In the /tag/tag-name part, it looks like the preceding subdirectory matches the first part of the final path segment - is that significant? "change the + signs to - signs" - could there be more than one + sign? Or is it literally just first+second? Or even hardcoded tag+name?? – MrWhite Jun 21 '18 at 9:22
  • 1
    @MrWhite in the WordPress example tag is literal and tag-name is a URL slug. I'm taking it as tags/tagged/tag is literal and tag+name is a URL slug with possibly multiple plusses. – Stephen Ostermiller Jun 21 '18 at 9:29
  • @MrWhite - the /tags/tagged was created by an external plugin from the old CMS, theyre not actual subdirectories. If a tag name has a space in it, the old CMS would automatically added the + sign between the words WordPress uses the - sign for their slugs – APadmin Jun 21 '18 at 9:40
1

You should be able to use something similar to the answer to mod_rewrite: replace underscores with dashes on StackOverflow:

RewriteEngine On
RewriteRule ^(/?tags/tagged/tag/[^/]*)\+([^/]*)$ $1-$2 [N]
RewriteRule ^/?tags/tagged(/tag/[^/\+]*)$ $1 [R=301,L]

The first rule should loop multiple times because it uses the N or "next" flag. It should change one plus to a a dash each time.

Once there are no pluses left, the second rule should trigger. It will remove the extra directories because the first parenthesis only includes the tag directory. Then it will issue the redirect.

  • I tried this, but it doesn't seem to be working/redirecting. I'm getting a 404 error :( – System Administrator Jul 9 '18 at 18:39
  • What if you remove the \+ from the last rule? That should issue the redirect without fixing the plusses. If that doesn't work, I think you don't have the code in the correct file or something. – Stephen Ostermiller Jul 9 '18 at 20:14

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.