1

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

3
  • 1
    Is that really how you have formatted the code? Please edit to correct since you appear to have erroneous spacing in several places - or are these just typos in the question?! Does this file (/images/qr/brochure.pdf) exist? What other .htaccess files do you have and what other directives? Why are you using a .htaccess file in that subdirectory and not the root?
    – MrWhite
    Feb 27, 2023 at 16:48
  • 1
    Aside: If this file does not exist, why are you wanting to redirect (to a non-related page) and not serve a more meaningful 404?
    – MrWhite
    Feb 27, 2023 at 16:50
  • @MrWhite, yes, that's exactly what I have on the file, it's the first time I'm using this, and haven't done an htaccess file before, I have used other info that I have found on the internet, just replacing it with my links (but it's not working). The file exists but we have sent that link in a promotional by accident and now I'm trying to redirect it to home, and will have the pdf in a different url
    – shiny01
    Feb 27, 2023 at 18:08

2 Answers 2

4

Place a new .htaccess file in the root of your documents folder within your web server or edit the existing one if you already created one.

For each PDF file, place a line in the .htaccess file with the following syntax:

Redirect 301 /currentfilepath/some-document.pdf http://example.com/index.php

The Redirect directive requires mod_alias to be loaded (it normally is on, but check).

The 301 indicates that this is a permanent redirection (in contrast to 302, which would be a temporary redirection). After that is the old path of the document (relative to the location of the .htaccess file). And the last argument is the new site location.

Restart the webserver after adjusting the .htaccess file, as it is most likely cached.

Hope this works for you.

1
  • "relative to the location of the .htaccess file" - You're thinking of the RewriteRule directive. The Redirect directive takes a document-root relative URL-path (regardless of the location of the .htaccess file). You shouldn't be redirecting to index.php here. The OP gave the "home page" as simply https://example.com/ and no mention of the use of PHP (why not use the URL-path given as an example in the question?). It would also be useful to state why Redirect may (or may not) be preferable here.
    – MrWhite
    Feb 14 at 17:52
1
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.

1
  • 1
    thank you for your detailed explanation! it worked!! we prefer the redirect against showing them an error page on the site, the link that will take users to the brochure (now the redirected link) is not directly visible to the visitors, so instead of showing an error page, we just want them to go home. Again, thank you so much for taking the time to help me! I have learned good things from your answer
    – shiny01
    Feb 27, 2023 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.