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

Apache HTTPD 2.4.x: How to "direct" users to a a custom error page if certain characters are present in the URL/URI?

· 52CE 我爱测

so I have this apache config where I'm trying to "rewrite" users to a custom error page but it's not working. I entered http://localhost/index.html/ddgdg%:sdsdfs in the browser and I didn't get my error page. I can view the custo…

问题原文

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

so I have this apache config where I'm trying to "rewrite" users to a custom error page but it's not working. I entered http://localhost/index.html/ddgdg%:sdsdfs in the browser and I didn't get my error page.

I can view the custom error page by directly entering its URL in the browser (http://localhost/my-error.html).

Any ideas what I could be doing wrong?


my full httpd.conf: link to httpd.conf


my changes to the default httpd.conf (I also had to uncomment the LoadModule for mod_rewrite.so)

RewriteEngine On
LogLevel alert rewrite:info
#if invalid characters are present in the URI, display 404 page
RewriteCond %{REQUEST_URI} ^\/.+[:%].+  
RewriteRule "^/$" "http://%{SERVER_NAME}/my-error.html" [L,R=301]

contents of custom error page

$ cat /Library/WebServer/Documents/my-error.html 
<html><body><h1>404 error here</h1></body></html>

apache version

$ apachectl -v
Server version: Apache/2.4.34 (Unix)
Server built:   Feb 22 2019 20:20:11

edit1: Running macOS Mojave at the moment but will setup an Ubuntu VM in awhile.

已采纳解答

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

There's a few different issues going on here...

http://localhost/index.html/ddgdg%:sdsdfs

You've not actually stated what response you are getting, just what is not happening. However, due to the stray % (not preceding a hexadecimal encoded octet), this URL is strictly invalid and I would expect Apache to respond with a 400 Bad Request. The only way to override this is to create a custom 400 error document in which you check the requested URL and customise the response. For example:

ErrorDocument 400 /my-error.html

If it wasn't for the stray % you should be able to catch this request using mod_rewrite and redirect accordingly. However, your RewriteRule is checking for an empty URL-path (ie. "^/$"), whereas the requested URL in your example is far from empty (ie. /index.html/ddgdg%:sdsdfs) - so the RewriteRule directive will never match your example URL. To check for % or : anywhere in the URL-path and redirect you could do something like the following:

# Checks for a "%" or ":" in the URL-path
RewriteRule [%:] /my-error.html [L,R=302]

But note that the URL-path matched by the RewriteRule pattern (as well as the REQUEST_URI server variable) is already %-decoded, so this will only match URLs where special characters have been doubly encoded (rare). (As noted above, an otherwise stray % will likely generate a 400 response before mod_rewrite is able to process the request.)

I would also question why you are wanting to "redirect" to your custom error document (ie. my-error.html) and not serve it directly? Redirecting has a number of disadvantages: 3xx response sent to the client, lost information regarding the URL that caused the error, doubling the requests to your server, etc.

You could internally rewrite the request to /my-error.html, instead of redirecting, by simply removing the R flag. For example:

RewriteRule [%:] /my-error.html [L]

But, unless you manually set the HTTP status in my-error.html then the user will see a 200 OK response - which is not desirable.

OR (preferably) create a custom 404 (which looks like what you are trying to do) and trigger that instead. For example:

ErrorDocument 404 /my-error.html

RewriteRule [%:] - [R=404]

Apache then sets the "404 Not Found" HTTP response status.

However, you probably don't need to use mod_rewrite at all here. In your example URL, everything after index.html in the URL-path, namely /ddgdg%:sdsdfs, is additional pathname information (aka path-info / PATH_INFO). By default, the handler that processes text/html responses does not permit path-info and will implicitly trigger a 404 (if it wasn't for the stray % - as discussed above), calling your custom ErrorDocument (if defined). So, the RewriteRule directive in the last example can simply be removed, since Apache will trigger a 404 anyway (unless you override this behaviour with AcceptPathInfo).

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

资料来源与适用范围

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

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

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