I would like to add a 301 redirect when users visit a specific page and redirect them to a PDF file.
In my .htaccess file I tried something like this this but it gives me a 404 error. However if I visit the path, the PDF it’s there
Redirect 301 /subpage https://www.mywebsite/upload/files/file.pdf
What am I doing wrong?
PS: I am using WordPress
3
Answers
To target an HTML link to a specific page in a PDF file, add #page=[page number] to the end of the link’s URL.
For example, this HTML tag opens page 4 of a PDF file named myfile.pdf:
And If you want to redirect a single page to another you just need to insert this line in the .htaccess file:
Redirect 301 /old-post https://www.yourwebsite.com/myfiles/myfile.pdf
Going to replace the old and new addresses respectively.
The code must be inserted after and before .
Redirect in htaccess does not matter if it is a pdf file or anything else just a valid link.
This is an example of a 301 redirect code to the link you want
RedirectMatch 301 /subpage/ https://www.mywebsite.com/upload/files/file.pdf
You are getting a
404
probably because the URI/subpage
doesn’t exist or map to an existent file. TheRedirect
you are using doesn’t redirect the/subpage
to the PDF location because your WordPressRewriteRules
override it. If you want to fix it , you will need to useRewriteRule
directive instead ofRedirect
at the top of your htaccess :