I'm running WordPress and about a month ago I needed to change the plugin that handles my tags. One thing I didn't notice until just recently was the manner in which the two different plugins handled spaces in tags. One placed a '+' sign between words while the new one places an '_'. I've recently started generating large amounts of 404s as a result and am looking to solve the issue using .htaccess to redirect the traffic to the right spot. I believe that .htaccess is the way to go, the problem is I just can't seem to wrap my head around it. Here's what I need to do ... Whenever I get a request for 'myURL.com/tag/foo+bar' I need to redirect to 'myURL.com/tag/foo_bar'. I've been scouring Google, these forums and other sites for a couple hours now but have had no luck finding anything applies. Can anyone provide me with a solution? I'd really appreciate it.
You can do this in mod_rewrite but it's not ideal. Unfortunately there is no such command to replace a certain character with another and therefore you'd need to create regex patterns for every number of + there may be in the URL. If you're only ever expecting one +, you can use: RewriteRule ^tag/(.+)\+(.+)$ tag/$1_$2 [R=301] If there may be two +, you'd need another rewrite rule to match that as well: RewriteRule ^tag/(.+)\+(.+)\+(.+)$ tag/$1_$2_$3 [R=301] And so on. As I'm sure you can imagine, PHP would quickly become a better solution: if ( strpos($_SERVER['REQUEST_URI'],'+') ) { header('Location: http://www.domain.com' . str_replace('+','_',$_SERVER['REQUEST_URI']); exit; } PHP:
As a rule I keep all my tags to two words - it makes it easier to sort them. I'm guessing that makes the first line the way to go for now. Unfortunately it's not redirecting - just sending me to a 404 page with 'myurl.com/tag/foo+bar' as the address. I added the line just as it appears above. added With the PHP solution you suggested - where would I add that to get it working?
You need to setup rewriting first. The whole thing would be something like: Options +FollowSymlinks Options +Indexes RewriteEngine on RewriteBase / RewriteRule ^tag/(.+)\+(.+)$ tag/$1_$2 [R=301] Code (markup): And that would go in a domain.com/.htaccess file.
Thanks again for your response rodney, I just tried that code as well and I'm still getting a 404 error. It's throwing me for a loop. There's something about the first part of the Rewrite Rule that doesn't like the + sign.