Forum Moderators: phranque
Here's the .htaccess code i've been using for a long time... now that there's a problem with google and 301s i'd like to find another way to do this because my pages are now starting to fall off.
Here's my code:
RewriteEngine On
RewriteRule ^(.*)\ (.*)$ $1-$2 [E=QR:$1-$2]
RewriteCond %{ENV:QR}!^$
RewriteCond %{ENV:QR}!(\ )
RewriteRule .* /%{ENV:QR} [R=301,L]
Basically my code replaces spaces with hyphens. I'd like to get a silent redirect to work, but when I take out the "R=301" it doesn't replace the spaces anymore. I have the .htaccess file in the root of my site.
Thanks for the help,
Peter
# Change one space to a hyphen, then restart mod_rewrite to
# change another space to a hyphen
RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [N]
If you also want search engines to change your listed URLs from spaces to hyphens, then you'll need one external 301 redirect at the end of the replacement process:
# Change one space to a hyphen, set a flag to indicate that
# a redirect will be needed, then restart mod_rewrite to
# change another space to a hyphen
RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [E=replaced_space:yes,N]
#
# If any spaces were replaced, do an external 301 redirect
RewriteCond %{ENV:replaced_space} ^yes$
RewriteRule . http://www.example.com/$1 [R=301,L]
Also, if you have, say, six or fewer spaces possible in the URL, it might be faster to just use three rules:
RewriteRule ^([^\ ]*)\ ([^\ ]*)\ ([^\ ]*)\ (.*)$ $1-$2-$3-$4
RewriteRule ^([^\ ]*)\ ([^\ ]*)\ (.*)$ $1-$2-$3
RewriteRule ^([^\ ]*)\ (.*)$ $1-$2
Adding in the redirect to fix search engine listings:
RewriteRule ^([^\ ]*)\ ([^\ ]*)\ ([^\ ]*)\ (.*)$ $1-$2-$3-$4 [E=replaced_space:yes]
RewriteRule ^([^\ ]*)\ ([^\ ]*)\ (.*)$ $1-$2-$3 [E=replaced_space:yes]
RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [E=replaced_space:yes]
#
RewriteCond %{ENV:replaced_space} ^yes$
RewriteRule . http://www.example.com/$1 [R=301,L]
Thanks again!
Peter
However, of these, HTTP/1.0 supports only 301 and 302.
For compatibility purposes, you're well advised to stick to those.
These '301 problems' are caused by the fact that people put site-wide 301s on domains that have been thoroughly indexed under both www- and non-www, and after having allowed other sites to link to both versions of the URLs for years. This confuses the search engines. If you put a canonical domain redirect in place before going live with your site, you won't ever have a bit of trouble.
Others cause problems because their 301s are "mis-implemented."
Unless you are redirecting many dozens or hundreds of URLs, I wouldn't worry about it, especially if the new URLs have already been found and indexed, most of your links point to the new URLs rather than the old, and the new URLs out-rank the old ones. If not, hold off on the redirects until this is the case, or until you see signs of duplicate-content trouble.
Jim