Pregunta

I've inherited a site with hundreds of scattered HTML and non-framework PHP files, which I am porting to Ruby on Rails 3.0.

As functionality is added in the Rails app, the corresponding pages are deleted from the document root; but, because there are often links to these in Google or from external sites, simply returning a 404 is not acceptable.

A URL like '/contact.php' should redirect to '/app/contact/', for example.

For the first few cases of this, I created simple stub html files at the old locations, with Meta tags within to perform the redirect. This doesn't scale well, particularly once I start replacing product pages, of which there are thousands.

My preference is to delete the old pages, then have the 404 handler dispatch these to the new Rails app, which will examine the URL using regexes and database lookup to try to figure out what the replacement page is, then issue a 301 redirect to that new page.

In httpd.conf, I placed the directive:

ErrorDocument 404 /app/error/handle404
# /app/error is a rails url.

When I hit "http://localhost/does-not-exist", this causes my ErrorController to be invoked, as expected.

However, within the controller, I cannot find the original path ("/does-not-exist") anywhere in request, request.headers, or ENV - I've been calling likely methods like request.request_uri (which contains /app/error/handle404), and examining request.headers and ENV without finding the expected original path.

The Apache access_log shows only the request for /does-not-exist, indicating that it transparently invoked /app/error/handle404 (without doing a redirect or causing a second request to be made).

How can I get access to the original URL?

Edit: to clarify, here is the sequence of events:

  1. User hits legacy path like http://mysite/foo.php, probably coming from some ancient link from a blog.
  2. ...but foo.php no longer exists!
  3. this is a 404, thus Apache invokes ErrorDocument
  4. directive is "ErrorDocument 404 /railsapp/error/handle404"
  5. Rails routes this to ErrorController action "handle404" - this is working correctly
  6. problem: in ErrorController, request.request.uri, request.headers do not provide any clue as to which URL the user was actually trying to get to, like "/foo.php"; I need to know the original URL to serve up an appropriate replacement page.
¿Fue útil?

Solución

As I couldn't find the original, non-rewritten URL in the Rails request, I ended up doing it in PHP - plain, old-fashioned, non-framework PHP with explicit mysqli_*() calls.

The PHP error handler receives the necessary information in the $_SERVER hash; $_SERVER['REQUEST_URI'] contains the original URI that I needed.

I look this up in a database, and if I find a corresponding entry, issue a 301 redirect to the new location; if there's no entry, I simply display a 404 page to the user.

Simplified (PHP):

$url = $_SERVER['REQUEST_URI'];
$redir = lookupRedirect($url);   # database stuff here
if (! $redir) {
    include ('404.phtml');
} else {
    header("Status: 301");
    header("Location: " . $redir['new_url']);
}

It's an ugly kluge, but I just couldn't find a way to make the Rails app aware of the error URL.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top