In previous articles, we have talked about 301 redirects, and why they are important for SEO.
Essentially, any time you have an external link built to a page or resource, you want that link to continue resolving to a page.
Unfortunately, what happens a lot of the time, especially in organizations that have been around for many years, is a phenomenon called link rot.
This is when links get broken over time, because URLs have changed, and no one has built redirects to new web addresses.
One Thing You May Not Know About Redirecting PDFs to New URLs
Here’s one things that I noticed when trying to redirect a PDF URL to a new URL. On many servers, using
.htaccess redirects, or even a plugin like Redirection will have no effect. This is because most servers are configured to resolve directly to the PDF URL.
Now, if you are on a Linux server, doing a 301 redirect for a PDF can be done by editing the
301 Redirecting a PDF on a Linux Server
.htaccess file is a special file at the root of your hosting instance. You can usually access and edit this through SFTP, SSH, or a File Manager (if your hosting provider has this in your admin panel).
This is by far the most preferable route to go. Simply add the line below to your
Redirect 301 /olddoc.pdf http://yoursite.com/newpage.html
The first part of the redirect says this is a permanent redirect, the next part is the legacy PDF URL. The last part is the new URL you want to the PDF to resolve to.
But, if you have an Apache server, the process gets a little more complicated. (Don’t worry though, you’ve still got this!)
Redirecting a PDF URL via PHP
Many hosts use Apache for their server. Many of the managed WordPress hosts actually use an Apache server with an NGINX layer on top for the admin. The point is, redirecting a PDF to a new URL is not as simple as setting up other 301 redirects.
Luckily, I found there is a clever workaround to this dilemma. (I didn’t think of it, but I’m happy to share how it works).
In this method, we will use PHP to redirect the old PDF address to your new web address.
Step 1: Rename your old PDF file. The reason for this is, we are going to set up a pathway for the server to process, so there cannot be a name conflict. If the PDF you are trying to redirect is named
mydocument.pdf, you can rename it something like
Step 2: Go to the directory path where your old PDF is, and create a new directory with that PDF’s name. In other words, if your legacy PDF was named
mydocument.pdf, then that is exactly what the name of your new directory is named. So far, so good.
Step 3: Crate a file in that new directory called
Step 4: Add the following code to the
index.php file and save it back to the server. Where it says Location, that’s where you add whatever URL you want to 301 redirect the old PDF towards.
<? Header( "HTTP/1.1 301 Moved Permanently" ); Header( "Location: http://www.yoursite.com/the-new-url-you-are-pointing-at" ); ?>
What we are essentially doing here is forcing the server into reading the new directory path as the old PDF web address. The server then reads the
index.php file and does a 301 redirect to the new URL.
If the old file path was
http://www.yoursite.com/pdf/mydocument.pdf, the new path is
http://www.yoursite.com/pdf/mydocument.pdf/index.php, which does the 301 redirect.
This is really useful if you have a bunch of old back links built up, which you cannot change, but need to redirect to a new resource.