php - 如何让 .htaccess 将 REQUEST_URI 附加到 404 页面

标签 php .htaccess mod-rewrite errordocument

有没有办法将请求 URI 作为 URL 变量发送到 404 页面?例如,如果我使用 ErrorDocument 指令转发我的 404,有没有办法做这样的事情?这是我试过的代码,但显然没有用。

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

我也试过 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]

基本上,我要做的就是当用户输入类似 http://mysite.com/asdf 的内容时,它会转发到 http://mysite。 com/pages/errors/index.php?e=404&url=/asdf 假设目录 /asdf 在服务器上不存在。

有没有简单的方法可以做到这一点?

最佳答案

我认为您不必将任何附加信息传递给错误处理程序脚本,如果这是您的问题。 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.

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

检查这个link

关于php - 如何让 .htaccess 将 REQUEST_URI 附加到 404 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14533020/

相关文章:

svn - 重写中断 svn

基于 PHP 类 OOP 的 Apache Mod 重写系统(固定链接 - 友好的 URL)

php - 包括其中包含函数的 PHP 文件是否会减慢包含那些即使不被使用的页面?

wordpress - 如何将 woocommerace 网站从 http 转移到 https

apache - .htaccess到其他端口的特定URL

.htaccess 重写类别和产品的规则

php - 导出 CSV 中的选定行并导入回同一 CSV 文件以更新 PHP/MySQL 中的选定行

php - 使用 javascript 为 1 台以上打印机进行打印

apache - 为什么 RewriteCond %{SCRIPT_FILENAME} !-d 会破坏我的 RewriteRule

php - 使用 .htaccess 创建 SEO 网址