开发者

Apache rewrite for 404: getting the requested url

开发者 https://www.devze.com 2023-03-08 07:58 出处:网络
On my error page that I redirect to for 开发者_如何学编程any 404s, I\'d like to record the url that the user tried to get to.

On my error page that I redirect to for 开发者_如何学编程any 404s, I'd like to record the url that the user tried to get to.

I've tried this but it doesn't work:

ErrorDocument 404 /error/?url=%{HTTP_REFERRER}

Can anyone tell me how to do it?


Try it with %{REQUEST_URI}. I'm not certain this will work in ErrorDocument since I've never tested it, but it's worth trying.

ErrorDocument 404 /error/?url=%{REQUEST_URI}


There isn't a direct way. Nor a perfect one. But there are few workarounds with PHP.

For example, I currently use a function to create the links of each page. So I would just need to add file_exists() to the main function (few lines in a single function).

This is the function I would use to create urls:

function url ($Value)
  {
  // Do some stuff with the url
  // [Not showed]

  if (!file_exists("/internal/path/".$Value))
    {
    // Call a function to store the error in a database
    error ("404", $Value);

    // One way of handling it. Replace '/' for ' ' and search that string.
    // Example: "path/to/page" would become "path to page".
    $Value=str_replace("/","%20",$Value); 
    return "http://www.example.com/search=".$Value;
    }
  else
    {
    // If the page exists, create the normal link.
    return $FullUrl;
    }
  }

This is my regular way of creating an urls:

<?php url('path/to/page'); ?>

I just thought about this method. It's great as it allows you to find missing pages even IF the user doesn't click on the links. Thank you for making me think about it and now I'll use it in my page (:

Another 'simpler' method (in case you do not wrap links) is that you store last couple of pages visited in $_SESSION['lastpage']; and $_SESSION['lastlastpage'];, if 404 is found then store the corresponding page from which the user tried to access the broken page. It's not a perfect solution since you have to manually find the broken link in the previous page, but at least it gives you some idea of where it is.

Disadvantage: As you can see, both solutions ONLY work with internal broken links.


It would seem there isn't a way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号