Forum Moderators: phranque

Message Too Old, No Replies

301 to silent redirect problem

         

Peter08

7:31 pm on Aug 23, 2006 (gmt 0)

10+ Year Member



Hey everyone,

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

jdMorgan

9:18 pm on Aug 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why do you have two rules, and why do you need the complication of defining and using the QR variable?

Do you want an external redirect --that will update search engine listings-- or an internal rewrite, so that you can continue to use URLs with spaces in them?

Jim

Peter08

1:52 pm on Aug 24, 2006 (gmt 0)

10+ Year Member



Thanks for the Quick Response!

I'm not really sure what you're asking, whats the difference between internal and external?

I guess it doesn't really matter...the goal is to replace all spaces with a hyphen without doing a 301 or 302 redirect.

Thank You,

Peter

jdMorgan

11:58 pm on Aug 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you just want requests for URLs with spaces to be served from files of the same name, but using hyphens instead of spaces, then use this:

# Change one space to a hyphen, then restart mod_rewrite to
# change another space to a hyphen
RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [N]

The rule above should be as close to the top of the file as possible, because it restarts mod_rewrite after finding a space, replaces another space, restarts again, etc., until no more spaces are found.

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

That will replace from one to six spaces without restarting, and may be a lot faster on your server.

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]

Jim

Peter08

2:36 am on Aug 25, 2006 (gmt 0)

10+ Year Member



Just out of curiosity is there any other way besides a 301 (or 302) to do an external redirect? The reason I ask is there's a lot problems with 301s with google right now... see this thread [webmasterworld.com...]

Thanks again!

Peter

jdMorgan

12:35 pm on Aug 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



HTTP/1.1 [w3.org] defines four main external redirect resposes, 301, 302, 303, and 307.

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