问题原文
原帖链接 · 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
Onis 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
