I deleted all my wordpress tags via phpmyadmin, and most of my post content contains tag links, now I redirected all the tag links to search using .htaccess,


example.com/tag/stack-exchange/ will be redirected to example.com/search/stack-exchange/

It worked fine on some links, but I notice that some of the links does not pull out post title due to the - sign on the search link url, If i replace the - with + it will find my post content (title), but if it is - sign on the link url its does not pull out my post content when it is searched.

Please I there any way I can redirect all the search urls containing - on the link url to +

I.e redirecting example.com/search/stack-exchange/ to example.com/search/stack+exchange/

.htaccess is preferable.

1 Answer 1


You can do something like the following at the top of your .htaccess file to replace all - with + that occur after the /search/ path segment in the requested URL:

RewriteRule ^(search/[^-]*)-([^-]*-.*) /$1+$2 [N]
RewriteRule ^(search/[^-]*)-([^-]*)$ /$1+$2 [R=302,L]

The first RewriteRule loops (internally) until all except 1 hyphen has been replaced with +. The second RewriteRule triggers an external redirect, replacing the last hyphen. In your example, where there is only 1 hyphen then the redirect will occur immediately.

Change the 302 (temporary) redirect to 301 (permanent) - if that is the intention - only after you have tested that it works OK.

UPDATE: If you have a problem with a rewrite loop (and consequently no redirect) when there are multiple hyphens (-) in the requested URL then you probably have an issue with PATH_INFO being appended to the target URL on each iteration of the loop. This can be fixed with the DPI (Discard Path Info) flag on the first RewriteRule directive. For example:

RewriteRule ^(search/[^-]*)-([^-]*-.*) $1+$2 [N,DPI]
RewriteRule ^(search/[^-]*)-([^-]*)$ /$1+$2 [R=302,L]

The slash prefix on the first RewriteRule substitution is then not required.

  • Thanks alot, your code It worked, but if the link url contains more than 2 words, it will not redirect to the + permalink on the url. After your code, example.com/search/stack-exchange/ redirects to example.com/search/stack+exchange/ but if the url contains links like example.com/search/stack-exchange-help/ it will will NOT redirect to the + url. Is there any Fix for this ? Commented Sep 23, 2018 at 8:38
  • You may need to add the DPI flag on the first RewriteRule directive if you are getting a rewrite loop (this seems to be somewhat dependent on the specific config of the site/server) - I've updated my answer. Or there may be a conflict with other directives in your .htaccess file, in which case please include your entire .htaccess file in your question (with these directives in-place).
    – MrWhite
    Commented Sep 23, 2018 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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