大网数据广告活动
网站运营与故障问答

Possible to match special characters with mod_alias and `Redirect` directive?

· 52CE 我爱测

I’m using Apache 2.4.6 with mod_alias and have a website which is receiving traffic from a link on another website that I have no control over. The other site’s link is poorly formed and it causes visitors that come to my site fr…

问题原文

原帖链接 · Wesley · CC BY-SA 3.0

I’m using Apache 2.4.6 with mod_alias and have a website which is receiving traffic from a link on another website that I have no control over. The other site’s link is poorly formed and it causes visitors that come to my site from the external site to receive an immediate 400 error. The url ends in a literal % sign for some reason: domain.com/news/october_news.pdf%

(Side note: Suffice it to say that the originating website cannot be contacted and will not change the URL for reasons unknown.)

My site heavily uses mod_alias and the Redirect directive, so I was hoping to maintain consistency and use the same directive for the benefit of future webmasters. From my reading of the documentation it would seem possible, but I’ve been unable to get it to work. I’m attempting to 301 redirect anyone that goes to the URL domain.com/news/october_news.pdf% to domain.com/news/october_news.pdf. Basically I need to hack off the percentage sign. How would I go about that with mod_alias and Redirect? Or perhaps it simply cannot be done and I need to use RedirectMatch.

I’ve currently attempted each of the following in an .htaccess file:

redirect “/news/october_news.pdf%” “/news/october_news.pdf”
redirect “/news/october_news.pdf%25” “/news/october_news.pdf”
redirect “/news/october_news.pdf\%” “/news/october_news.pdf”

All of which have not changed the 400 Bad Request error that Apache returns to the browser.

已采纳解答

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

You can't use a mod_alias Redirect (or even mod_rewrite RewriteRule - which executes before mod_alias) to catch a 400 "bad request". This applies to directives in both .htaccess and in the server config. The "bad request" is simply triggered much earlier in the request.

In fact, you can't do anything in .htaccess to trap the bad request. .htaccess is never even processed - it's too late.

However, as @Stephen suggested in comments, you can define a custom 400 error document in the server config and examine such requests using your server-side script (eg. PHP) to then issue an overriding redirect. But this error document must be defined in a server or virtualhost context; .htaccess (or <Directory> containers) is too late. For example, directly in your <VirtualHost> container:

ErrorDocument 400 /errordocs/e400.php

Then, in the case of PHP, you can examine the requested URL (containing the trailing %) using the REDIRECT_URL (or REQUEST_URI) indices of the $_SERVER superglobal and issue the appropriate redirect.

Make sure your default 400 response returns a reasonable / user friendly message with appropriate links, otherwise you'll likely get a browser default.


redirect “/news/october_news.pdf%” “/news/october_news.pdf”

Aside: You should not be using curly quotes “..” to surround the arguments - the request will never match (for this reason also). If you need/want to surround the arguments then you must use straight double quotes, ie. ".

This is also a 302 (temporary) redirect (maybe you did this just to test?). This should ultimately be a 301 (permanent) redirect.

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

资料来源与适用范围

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

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

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