views:

660

answers:

4

Hi all,

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:

  1. 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.

  2. 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:

  1. How to get the rewrite rule to work
  2. How to do a '301' with Zend_Controller_Router_Route if possible.
  3. Is there another way that I'm missing?
  4. Which is the better way and why?
  5. Why isn't google figuring this out? It has been nearly half a year.
+2  A: 

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.

Greg
Thanks for the quick reply, giving it a go now.
karim79
I did that, but it did not work, exactly the same result as before. Any idea?
karim79
Did you put it before the other rule?
Greg
No, I put it after. Will try it again, thanks.
karim79
Tried. It seems to know when I enter the DEFAULT.ASP URL, but browser says 'could not connect to server'. Strange.
karim79
That's odd... See what Location header you're being sent (if you're getting that far).
Greg
Greg - my own stupid fault. I firebugged it, the 301 was happening, but apache was taking the host from the etc, which was incorrect. Thanks a gazillion!
karim79
+1  A: 
Redirect permanent /foo/DEFAULT.ASP /foo
duckyflip
Thanks. I tried that but got 'Internal Server error'!?
karim79
+2  A: 

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.

Steve
+1 - Thanks Steve, interesting way to solve the problem! I never would have thought of that!
karim79
+1  A: 

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.

joedevon