Have to look into the number of spaces and convert individuals to dashes in htaccess. You will find a number of different bad web addresses that I am redirecting and presently they work fine when the tag is a word... if there is a space it 404's with /word1%20word2 when it ought to be /word1-word2.

Now...nevertheless - there isn't always only one space... sometimes there's 2 but tend to depend on 6 so far as I am seeing in G.website owner tools conflicts.

My current rewrite that actually works for that url sequences below.

RewriteRule ^.+\(offset\)/+.*?/\(tag\)/([^.]+)$ /tag/$1 [R=301,L]

works best for: www.domain.com/index.php/Blog/(offset)/48/(tag)/word1

works best for: www.domain.com/Blog/(offset)/328/(tag)/word1

works best for: www.domain.com/Tags/(offset)/24/(tag)/word1

doesn't work for: www.domain.com/index.php/Blog/(offset)/48/(tag)/word1%20word2%20word3

doesn't work for: www.domain.com/Blog/(offset)/328/(tag)/word1%20word2

doesn't work for: www.domain.com/Tags/(offset)/24/(tag)/word1%20word2

SOO... I am beside myself trying to puzzle out how you can capture a mystery group of spaces and replace all of them with dashes. Halp!

I am less than confident that you'll be able to match a mystery quantity of whitespaces and replace then simply using mod_rewrite.

So one solution is always to redirect internally (with no R) to some simple PHP, pass the whitespace-separated value like a parameter towards the PHP script, perform the necessary substitutes in PHP code after which let PHP send an exterior redirect towards the remedied URL (by setting the header Location: /new-url), an identical approach is referred to here.

If you're searching for a pure .htaccess / mod_rewrite solution anyway, than the is something you might begin with:

RewriteRule ^temprewrite/([\S]+)$ /tag/$1  [R=301,L]
RewriteRule ^temprewrite/(.*?)\s(.*) /temprewrite/$1-$2
RewriteRule ^.+\(offset\)/\d+/\(tag\)/(.*) /temprewrite/$1
  • The 3rd line does the preprocessing by removing the whitespace-separated tag. It will get rewritten internally and can match either the very first or even the second rule.
  • The very first rule does the ultimate redirect if forget about whitespaces as matched up by [\S]+ are located.
  • The 2nd rule splits the temporary value into two parts separated through the first whitespace \s, concatinates the various components having a - and passes the end result to the rewrite engine, where either the very first or even the second rule will match again.

Please be aware this "code" isn't fully examined and can miss some rare cases and/or result in multiple internal rewrites, but It "is effective enough" to try it out.

well... it appears the code here works. 301 Redirect With Spaces

RewriteCond %{THE_REQUEST} (\s|%20)
RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI]

I've that rule AFTER my original one also it appears to become working all right. Hope this can help anybody determine the problem too. :)