What's the best way to issue a 301 redirect in this situation?
-
06-09-2019 - |
Question
I have a PHP5/Zend Framework 1.8.1 web site which is located at:
http://www.example.com/foo
The older version of this site, which infuriatingly, is still maintained by google's index was located at:
http://www.example.com/foo/DEFAULT.ASP
So a lot of people are requesting the above link and hitting a dead end.
I figure, the way to fix this would be to issue a 301 redirect to take him to the new site. The two ways to do this that spring to mind are:
The first way I thought of was to add a rewrite rule to our .htaccess . Our site implementation is PHP5 / Zend Framework 1.8.1, so there's an existing rewrite rule in the .htaccess (as per Zend Framework MVC requirement) which is:
RewriteRule !\.(js|ico|gif|jpg|png|css|jpeg)$ index.php
As a mod_rewrite noob, I did a bit of googling 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 those lines to the .htaccess, but they do not work.
The second way I thought of is to use
Zend_Router_Route_Static
as follows:$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 have no idea how to set a 301 header as well, plus it's quite inelegant having those lines of code in my
bootstrap.php
Can anyone offer any advice on how to deal with this situation? I would like to know all or any of:
- How to get the rewrite rule to work
- How to do a '301' with
Zend_Controller_Router_Route
if possible. - Is there another way that I'm missing?
- Which is the better way and why?
- Why isn't google figuring this out? It has been nearly half a year.
Solution
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 it out - are you sure you're giving a proper 404 status code? You can check by looking at the headers - in Firefox I use Live HTTP Headers.
OTHER TIPS
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 with php.
Then I created a file default.asp containing header( 'Location: /', etc ) to replace the one that Google was looking for. I had a few other .asp files with complex parameters that needed to be transformed into new URLs before redirecting, in some cases requiring a database lookup, so it was handy being able to write everything in PHP so I could use include files from other parts of the project.
Redirect permanent /foo/DEFAULT.ASP /foo
Looks like you solved your problem, and this wouldn't be a good solution for your issue, but here's another resource that others might find useful if they were attracted to this thread by your question:
http://www.refreshinglyblue.com/2008/10/28/zend-framework-redirect-the-easy-way/
He's suggesting the "Redirector Zend Controller Action Helper" to redirect internal urls.
P.S. If anyone does use _redirect, make sure to pass in the 301 redirect code (if that's what you want) since it defaults to 302.