Apache rewrite for 404: getting the requested url

烂漫一生 提交于 2019-12-13 00:34:51

问题


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?


回答1:


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}



回答2:


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.




回答3:


It would seem there isn't a way.



来源:https://stackoverflow.com/questions/6117589/apache-rewrite-for-404-getting-the-requested-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!