What's the best way to issue 301 redirects in this situation?
I have a PHP5 / Zend Framework 1.8.1 website located at:
http://www.example.com/foo
An older version of this site, which is annoyingly still maintained by the google index, was located at:
http://www.example.com/foo/DEFAULT.ASP
So many people ask for the above link and get stumped.
I suppose the way to fix this would be to issue a 301 redirect to send it to the new site. There are two ways to do this for spring to be able to:
-
The first way I thought was to add a rewrite rule to our .htaccess. Our site implementation is PHP5 / Zend Framework 1.8.1, so there is an existing rewrite rule in .htaccess (as required by Zend Framework MVC):
RewriteRule !\.(js|ico|gif|jpg|png|css|jpeg)$ index.php
As a mod_rewrite noob, I did a bit of a search and came up with the following to handle the redirect:
RewriteCond %{HTTP_HOST} ^/foo/DEFAULT.ASP RewriteRule (.*) http://example.com/foo$1 [R=301,L]
I added these lines to .htaccess but they don't work.
-
The second way I thought was to use
Zend_Router_Route_Static
like this:$router = $frontController->getRouter(); $route = new Zend_Controller_Router_Route_Static('foo/DEFAULT.ASP', array('controller' => 'foo', 'action' => 'index')); $router->addRoute('foo', $route);
This redirects to the correct page, but I don't know how to set the 301 header, and also rather inelegant having these lines of code in my
bootstrap.php
Can anyone suggest any advice on how to handle this situation? I would like to know all or any of:
- How to make the rewrite rule work
- How to make "301" s
Zend_Controller_Router_Route
if possible. - Is there any other way I'm missing?
- What's the best way and why?
- Why doesn't Google understand this? Almost six months have passed.
a source to share
This should work (make sure you put it before the Zend rule):
RewriteRule ^foo/DEFAULT.ASP /foo [R=301,L]
I'm not sure why Google isn't figuring this out - are you sure you are giving the correct 404 status code? You can check by looking at the headers - in Firefox I'm using Live HTTP Headers .
a source to share
I recently solved a similar problem by adding
AddType application/x-httpd-php .asp
to the .htaccess file which makes the server run .asp files from php.
Then I created a default.asp file containing a header ('Location: /' etc.) to replace the one that Google was looking for. I had several other .asp files with complex parameters that needed to be converted to new urls before redirecting, in some cases requiring database lookups, so it was convenient to write everything in PHP so that I could use the included files from other parts project.
a source to share
It looks like you've solved your problem and it won't be a good solution for your problem, but here's another resource that others might come in handy if they were attracted to this topic by your question:
http://www.refreshinglyblue.com/2008/10/28/zend-framework-redirect-the-easy-way/
It offers the Zend Redirector Administrator Action Helper to redirect internal URLs.
PS If anyone is using _redirect, be sure to pass a 301 redirect code (if that's what you want) as it defaults to 302.
a source to share