|Having problems with a 301 redirect|
Returns a 404
| 2:44 pm on Aug 16, 2010 (gmt 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
Thanks for your input!
| 7:47 pm on Aug 16, 2010 (gmt 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)|
Where is this code located?
| 1:42 am on Aug 17, 2010 (gmt 0)|
| 1:52 am on Aug 17, 2010 (gmt 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)|
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)|
|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)|
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.