大网数据广告活动
服务器运维问答

Apache .htaccess RewriteRule and ErrorDocument

· 52CE 我爱测

I have a .htaccess file : RewriteEngine On DirectorySlash Off RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.mp4|\.ttf|\.eot|\.woff)$ RewriteRule ^(.*)$ ?page=$1 [NC,L,QSA] ErrorDocument 404 /e404 ErrorDocument 403 /e404 E…

问题原文

原帖链接 · Sinf · CC BY-SA 4.0

I have a .htaccess file :

RewriteEngine On
DirectorySlash Off

RewriteCond %{REQUEST_URI}  !(\.css|\.js|\.png|\.jpg|\.mp4|\.ttf|\.eot|\.woff)$
RewriteRule ^(.*)$ ?page=$1 [NC,L,QSA]

ErrorDocument 404 /e404
ErrorDocument 403 /e404
ErrorDocument 500 /e500

And a PHP function which checks to see if the requested URI exists. If the URI exists in the database, it will get the contents and render the page, if not, it will get the 404 error page contents and render the page. (Everything is OK here)

The problem is when I use a URI with extensions which are inside the RewriteCond part of the .htaccess file, something like example.com/file.css. In this case, the page gets redirected to example.com/e404 (that's what I want). The problem here is:

I don't know how to get the "wrong URI" used before redirecting to example.com/e404, because when the error page is loaded, I have a function inside the page to insert the "wrong URI" in the database.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

Aside: The directives you've posted would create a rewrite loop on any default Apache installation, so maybe you have other directives or a different config that you aren't disclosing in your question? Anyway, ignoring this for the moment, since you don't appear to be having a problem like this.

I don't know how to get the wrong URI used before redirecting to example.com/e404 ...

But do you need to get this information before "redirecting"? In PHP you can simply check the $_SERVER['REQUEST_URI'] superglobal to get this information (the URL that resulted in the 404). However, note that this contains the slash prefix and query string (if any), which your page URL parameter would not otherwise contain.

Also, just to clarify terminology, this is not strictly a "redirect" to example.com/e404. A "redirect" implies an external HTTP redirect. Apache issues an internal subrequest for the error document (which is similar to a URL rewrite, but not quite).

One of the potential issues with your code is that the 404 response is served in a rather round-about way:

  1. If file.css does not exist then Apache triggers an internal subrequest for /e404
    (this triggers another round of processing...)
  2. Since /e404 does not end in one of the stated file extensions, it is further rewritten to ?page=e404 (presumably index.php?).

Because of the (unnecessary) 2nd rewrite, other server variables, such as REDIRECT_URL and REDIRECT_QUERY_STRING (which are also passed through to the PHP $_SERVER superglobal) are not set as expected. Ordinarily, these would be set to the "wrong URI" (ie. the requested URL that triggered the 404), but instead, they contain details of the error document itself.

You should do the above in 1 step and avoid the second rewrite. For example (assuming index.php is the file handling the request), the error document should be set to the desired end result:

ErrorDocument 404 /index.php?page=e404

Incidentally, you don't necessarily need to explicitly pass the HTTP "error status" (ie. e404) in the URL, as this is available in PHP's $_SERVER['REDIRECT_STATUS'] superglobal.

RewriteRule ^(.*)$ ?page=$1 [NC,L,QSA]

I've assumed you are rewriting to index.php (the registered DirectoryIndex)? Instead of falling back to let mod_dir issue an additional subrequest for the DirectoryIndex, you should be explicit and include the file you are rewriting to in the substitution. For example:

RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

(The NC flag is superfluous here.)

I don't know how to get the wrong URI used before redirecting to example.com/e404 ...

Stepping back to address your initial query again (as an academic exercise). You can do this on Apache 2.4.13+ (although I don't think you need to). On Apache 2.4.13+ you can create a dynamic ErrorDocument using Apache expressions.

For example, to explicitly pass the URL-path that caused the 404 error in the URL for the error document itself then you can do something like the following:

ErrorDocument 404 /index.php?page=%{escape:%{REQUEST_URI}}&error=404

The URL param page then contains the URL-path (which includes the slash prefix, unlike your rewrite that excludes it). An additional error URL param sends the HTTP status (although, as mentioned above, this is not strictly necessary).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-11-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。