Welcome to WebmasterWorld Guest from 54.161.21.157

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Having problems with a 301 redirect

Returns a 404

     
2:44 pm on Aug 16, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 1, 2002
posts:774
votes: 0


I am doing some shuffling on my site, and for various reasons I have moved a couple directories (and all their subpages) in my directory structure. To be kind to Goog and others, I have tried to run a 301 in Apache, thus:

RewriteRule ^old_path/Directory/(.*)$ new_path/_NEW_ Directory/$1 [L,R=301]

First, when I tested, I got 200's... and I realized I need to erase the old directory. SO I did. Now when I test, I get 404's....

As a variation, I tried:

Redirect 301 ^old_path/Directory/(.*)$ http:// www.mysite.com/ new_path/_NEW_ Directory/$1

Still 404ing...

Thanks for your input!

Dave
7:47 pm on Aug 16, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


RewriteRule ^old_path/Directory/(.*)$ http://www.example.com/new_path/_NEW_Directory/$1 [R=301,L]


The above code should work.

Clear your browser cache before testing.
8:17 pm on Aug 16, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Where is this code located?

Jim
1:42 am on Aug 17, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 1, 2002
posts:774
votes: 0


Hi Jim:

in httpd.conf

Dave
1:52 am on Aug 17, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 1, 2002
posts:774
votes: 0


Weird.... now it works. Thanks. Don't think it was cache, as I was using a "service" to check the header codes.... Anyway, I am seeing a 301 now...
1:56 am on Aug 17, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 1, 2002
posts:774
votes: 0


OK, found the problem. so simple... so stupid...

Not: RewriteRule ^old_path/Directory/(.*)$

But: RewriteRule ^/old_path/Directory/(.*)$

The slash must be in there it looks like...
8:34 am on Aug 17, 2010 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10887
votes: 72


The slash must be in there it looks like...

if the rule is used in the server config file (httpd.conf) then the path will contain the leading slash indicating the root directory.
if the rule is used in the per-directory config files (.htaccess) then the leading directory path information will be stripped before pattern matching.

Note: Pattern matching in per-directory context [httpd.apache.org]
Never forget that Pattern is applied to a complete URL in per-server configuration files. However, in per-directory configuration files, the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done. This feature is essential for many sorts of rewriting - without this, you would always have to match the parent directory which is not always possible.
2:47 am on Aug 19, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


In addition, if the rule is enclosed in a <Directory> container in a server config file, then the path specified by that container will also be removed from the path 'seen' by the RewriteRule.

Jim