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

How can I redirect a PDF to home page?

· 52CE 我爱测

I'm trying to redirect from an URL like this: https://example.com/images/qr/brochure.pdf to the home page of the site: https://example.com I tried using this code in a .htaccess file, and added the file in the same folder as the …

问题原文

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

I'm trying to redirect from an URL like this:

https://example.com/images/qr/brochure.pdf

to the home page of the site: https://example.com

I tried using this code in a .htaccess file, and added the file in the same folder as the brochure:

RewriteEngine On RewriteRule ^ brochure.pdf$ https://example.com/[R=301,L,NC]

but this only makes the brochure link act as if it doesn't exist but doesn't redirect it to home.

I also tried a couple of other codes that generate a 500 error on the page, so this is the closest I have gotten.

Also, if there is any other solution for this problem let me know, it doesn't have to be exactly on .htaccess

已采纳解答

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

RewriteEngine On RewriteRule ^ brochure.pdf$ https://example.com/[R=301,L,NC]

This is basically correct, except you have made (what looks like) a series of typos with the spacing/whitespace:

  • Each directive needs to be on a separate line. The above will do nothing since everything after On is simply ignored.
  • ^ brochure.pdf$ - There should be no space after ^. Spaces are argument delimiters in Apache config files, so this is now two arguments, not one. It should be ^brochure\.pdf$ (the literal dot should also be backslash-escaped, but this is not an "error" as such).
  • https://example.com/[R=301,L,NC] - You are missing a space before the flags argument. As noted above, spaces are argument delimiters. This would otherwise be seen as one argument and result in a malformed redirect (resulting in a 404).

Additionally,

  • R=301 - I would question whether this should be a 301 "permanent" redirect. This will be cached persistently by anyone requesting this URL, even after the redirect directive has been removed.

So, bringing the above points together, it should be written as follows if you are using the /images/qr/.htaccess file:

RewriteEngine On
RewriteRule ^brochure\.pdf$ https://example.com/ [NC,R=302,L]

Although, would a more meaningful "custom" 404 not be preferable? A 404 would generally make more sense to any user that happens to follow that link.

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

资料来源与适用范围

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

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

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