简体   繁体   English

如何获取 .htaccess 将 REQUEST_URI 附加到 404 页面

[英]How to get .htaccess to attach REQUEST_URI to 404 page

Is there a way to send the request URI along to the 404 page as a URL variable?有没有办法将请求 URI 作为 URL 变量发送到 404 页面? For instance, if I forward my 404's with an ErrorDocument directive, is there a way to do something like this?例如,如果我使用ErrorDocument指令转发我的 404,有没有办法做这样的事情? This is the code I tried but it obviously didn't work.这是我试过的代码,但显然没有用。

ErrorDocument 404 /pages/errors/index.php?e=404&url=%{REQUEST_URI}

I also tried a mod_rewrite, but I couldn't get that working either.我也尝试了 mod_rewrite,但我也无法让它工作。 Here is what I tried with mod_rewrite:这是我用 mod_rewrite 尝试的:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /pages/errors/index.php?e=404&url=%{REQUEST_URI} [L,R=404]

Basically all I'm trying to do is so that when a user types in something like http://mysite.com/asdf then it forwards to http://mysite.com/pages/errors/index.php?e=404&url=/asdf assuming that the directory /asdf does not exist on the server.基本上我想要做的就是当用户输入类似http://mysite.com/asdf它会转发到http://mysite.com/pages/errors/index.php?e=404&url=/asdf假设目录/asdf在服务器上不存在。

Is there an easy way to achieve this?有没有简单的方法来实现这一目标?

I don't think you have to pass any additional information to the error handler script, if that's what your question is about.我认为您不必将任何其他信息传递给错误处理程序脚本,如果这就是您的问题的话。 Apache supplies enough information: Apache 提供了足够的信息:

Redirecting to another URL can be useful, but only if some information can be passed which can then be used to explain or log the error condition more clearly.重定向到另一个 URL 可能很有用,但前提是可以传递一些信息,然后可以使用这些信息更清楚地解释或记录错误情况。

To achieve this, when the error redirect is sent, additional environment variables will be set, which will be generated from the headers provided to the original request by prepending 'REDIRECT_' onto the original header name.为了实现这一点,当发送错误重定向时,将设置额外的环境变量,这些变量将从提供给原始请求的标头生成,方法是在原始标头名称上添加“REDIRECT_”。 This provides the error document the context of the original request.这为错误文档提供了原始请求的上下文。

For example, you might receive, in addition to more usual environment variables, the following.例如,除了更常见的环境变量之外,您可能还会收到以下内容。

REDIRECT_HTTP_ACCEPT= / , image/gif, image/jpeg, image/png REDIRECT_HTTP_ACCEPT= / , 图像/gif, 图像/jpeg, 图像/png

REDIRECT_HTTP_USER_AGENT=Mozilla/5.0 Fedora/3.5.8-1.fc12 Firefox/3.5.8 REDIRECT_HTTP_USER_AGENT=Mozilla/5.0 Fedora/3.5.8-1.fc12 Firefox/3.5.8

REDIRECT_PATH=.:/bin:/usr/local/bin:/sbin REDIRECT_PATH=.:/bin:/usr/local/bin:/sbin

REDIRECT_QUERY_STRING= REDIRECT_QUERY_STRING=

REDIRECT_REMOTE_ADDR=121.345.78.123 REDIRECT_REMOTE_ADDR=121.345.78.123

REDIRECT_REMOTE_HOST=client.example.com REDIRECT_REMOTE_HOST=client.example.com

REDIRECT_SERVER_NAME=www.example.edu REDIRECT_SERVER_NAME=www.example.edu

REDIRECT_SERVER_PORT=80 REDIRECT_SERVER_PORT=80

REDIRECT_SERVER_SOFTWARE=Apache/2.2.15 REDIRECT_SERVER_SOFTWARE=Apache/2.2.15

REDIRECT_URL=/cgi-bin/buggy.pl REDIRECT_URL=/cgi-bin/buggy.pl

REDIRECT_ environment variables are created from the environment variables which existed prior to the redirect. REDIRECT_ 环境变量是从重定向之前存在的环境变量创建的。 They are renamed with a REDIRECT_ prefix, ie, HTTP_USER_AGENT becomes REDIRECT_HTTP_USER_AGENT.它们使用 REDIRECT_ 前缀重命名,即 HTTP_USER_AGENT 变为 REDIRECT_HTTP_USER_AGENT。

REDIRECT_URL, REDIRECT_STATUS, and REDIRECT_QUERY_STRING are guaranteed to be set, and the other headers will be set only if they existed prior to the error condition. REDIRECT_URL、REDIRECT_STATUS 和 REDIRECT_QUERY_STRING 保证被设置,并且其他标头只有在错误条件之前存在时才会被设置。

None of these will be set if the ErrorDocument target is an external redirect (anything starting with a scheme name like http:, even if it refers to the same host as the server).如果 ErrorDocument 目标是外部重定向(任何以 http: 之类的方案名称开头的内容,即使它指的是与服务器相同的主机),这些都不会被设置。

Check this link检查此链接

I think you need to remove R=404 .我认为您需要删除R=404 This worked for me;这对我有用;

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?e=404&url=%{REQUEST_URI} [L]

Tests: foo/index.php : print_r($_GET)测试: foo/index.php : print_r($_GET)

localhost/foo -> Array ( )

localhost/foo/asdf -> Array ( [e] => 404 [url] => /foo/asdf )

Your first attempt of putting %{REQUEST_URI} in the ErrorDocument directive should now work in a recent version of Apache.您第一次尝试将%{REQUEST_URI}放在ErrorDocument指令中,现在应该可以在最新版本的 Apache 中使用。 From https://httpd.apache.org/docs/2.4/mod/core.html#errordocument :来自https://httpd.apache.org/docs/2.4/mod/core.html#errordocument

From 2.4.13, expression syntax can be used inside the directive to produce dynamic strings and URLs.从 2.4.13 开始,可以在指令内部使用表达式语法来生成动态字符串和 URL。

I have another idea, since you're using php, why not just access $_SERVER['HTTP_REFERER'] .我有另一个想法,既然您使用的是 php,为什么不直接访问$_SERVER['HTTP_REFERER'] That shows you previous page request, so presumably it would be exactly what you're looking for.这显示了您之前的页面请求,因此大概这正是您要查找的内容。 From there, you can do what you want with that value.从那里,您可以使用该值做您想做的事情。 Does that work?那样有用吗?

You are alreay checking file/folder existing before rewriterule by rewritecond, you don't need to use request uri in rewrite rule and handle it by $_SERVER['REQUEST_URI']您已经通过 rewritecond 检查 rewriterule 之前存在的文件/文件夹,您不需要在重写规则中使用请求 uri并通过$_SERVER['REQUEST_URI']处理它

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /pages/errors/index.php?e=404 [L,R=404]

Finally got this working, albeit a bit unconventionally.终于得到了这个工作,尽管有点非常规。 I'm running on ColdFusion instead of PHP and the other answers here didn't work for me, unfortunately.不幸的是,我使用的是 ColdFusion 而不是 PHP,这里的其他答案对我不起作用。 But I think this will work for anyone regardless of their server-side platform because it's javascript based:但我认为这对任何人都适用,无论他们的服务器端平台如何,因为它是基于 javascript 的:

In .htaccess:在 .htaccess 中:

ErrorDocument 404 /mark404.cfm

In /mark404.cfm在 /mark404.cfm

<script>
   window.location = '/handle404.cfm';
</script>

In /handle404.cfm在/handle404.cfm

<script>
    var r = document.referrer;
    //this is what you want, the url that the user tried to hit
</script>

Reason this works is the browser still "thinks" it's on the requested doesntexist.html when it hits mark404.这样做的原因是浏览器在点击 mark404 时仍然“认为”它在请求的 dontexist.html 上。 So when you redirect to handle404, doesntexist.html is in the browser history and javascript can pick it up.因此,当您重定向到 handle404 时,donstexist.html 在浏览器历史记录中并且 javascript 可以获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM