htaccess errordocument 404 and pass url to path

一笑奈何 提交于 2019-12-02 10:43:19

问题


How can I pass the 404'd URL to my 404.html page using .htaccess

For example, if I visit an invalid page: /user/123

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ErrorDocument 404 /?404.php?error_path={???}
                                         ^
=========================================^ 

Resulting in a 404 redirect to /404.php?error_path=/user/123


回答1:


ErrorDocument is not part of mod_rewrite and that is invalid. Is this what your looking for?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewriterule ^(.*) /404.php?error_path=$1 [R=301,L]



回答2:


On apache 2.4 and later, you can use mod-rewrite variables with ErrorDocument directive :

ErrorDocument 404 /404.php?uri=%{REQUEST_URI}

This will internally send the 404 uri to your 404.php page and you can manipulate it using the following code in 404.php

<?php
echo "the broken uri is $_GET['uri']";
?>



回答3:


You can't append the path of the site which threw the error to the URL of the displayed error page using the ErrorDocument directive. However as you're working with PHP you could use the global $_SERVER["REDIRECT_URL"] value from within your error handling script.

See http://httpd.apache.org/docs/current/custom-error.html#variables for more information:

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.

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. 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_USER_AGENT=Mozilla/5.0 Fedora/3.5.8-1.fc12 Firefox/3.5.8
REDIRECT_PATH=.:/bin:/usr/local/bin:/sbin
REDIRECT_QUERY_STRING=
REDIRECT_REMOTE_ADDR=121.345.78.123
REDIRECT_REMOTE_HOST=client.example.com
REDIRECT_SERVER_NAME=www.example.edu
REDIRECT_SERVER_PORT=80
REDIRECT_SERVER_SOFTWARE=Apache/2.2.15
REDIRECT_URL=/cgi-bin/buggy.pl

REDIRECT_ environment variables are created from the environment variables which existed prior to the redirect. They are renamed with a REDIRECT_ prefix, i.e., HTTP_USER_AGENT becomes 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.

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



来源:https://stackoverflow.com/questions/22467935/htaccess-errordocument-404-and-pass-url-to-path

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