How to Redirect PDF Files with PHP, ASP or .htaccess for SEO

Return to Blog

This is the follow up to my previous post "SEO for PDFs - Optimizing PDF Files for Search Engines" in which I recommended NOT using PDF files whenever possible. In this post, I will explain how to go go about redirecting PDF files with .htaccess, PHP and ASP.

301 Redirects
When it comes to SEO, the best redirect to use is a 301 permanent redirect. This is because search engines still have trouble handling 302 temporary redirects. And don't even think about using a Meta Tag Refresh redirect or a redirect generated by a client-side language such as JavaScript if you care about search engine optimization.

.htaccess 301 Redirect
This is the easiest and most straightforward option to use, but it's only available on a Linux server. Simply add the line below to your .htaccess file.

Redirect 301 /oldfile.pdf http://www.example.com/newpage.html

PHP 301 Redirect
This is also normally a pretty easy way to redirect pages by adding a couple lines of PHP on each page you want to redirect.  But since PHP code cannot actually be inserted into the PDF file, we have to treat it a bit differently. Follow the steps below:

1. Rename oldfile.pdf file to filename2.pdf.
2. Create a new directory named "oldfile.pdf" in the same directory that the PDF is in.
3. Add an index.php file in the new oldfile.pdf directory.
4. Add the following PHP code to the top of the index.php. file:

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.example.com/new-page-to-redirect-to.php" );
?>

Now when the search engines access http://www.example.com/oldfile.pdf they will actually be served the http://www.example.com/oldfile.pdf/index.php file which contains the 301 redirect to the new page.

ASP 301 Redirect
This is very similar to the steps for the PHP 301 redirect above, but this is websites that are hosted on Windows servers and use ASP.

1. Rename oldfile.pdf file to filename2.pdf.
2. Create a new directory named "oldfile.pdf" in the same directory that the PDF is in.
3. Add an index.asp file in the new oldfile.pdf directory.
4. Add the following ASP code to the top of the index.asp file:

<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.example.com/new-page-to-redirect-to.asp"
%>

Now when the search engines access http://www.example.com/oldfile.pdf they will actually be served the http://www.example.com/oldfile.pdf/index.asp file which contains the 301 redirect to the new page.

I just realized I haven't explained how to identify PDFs which are already ranking in the search engine results and thus are prime candidates for redirecting, so I'll do that in another follow up post shortly!