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

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!

7 Comments so far
  1. by Jay

    On November 23, 2009 at 10:50 am

    How does one redirect multiple page in asp to a new Unix server?

  2. by admin

    On November 24, 2009 at 11:21 am

    Hi Jay,

    I would recommend using .htaccess on the new Unix server to redirect any old URLs which were made in .asp. You can do so by adding the following line to your .htaccess file for each of the pages:

    Redirect 301 /oldfile.asp http://www.example.com/newpage.php

  3. by Ed Jarvis

    On May 5, 2011 at 5:27 am

    When I try to access http://www.example.com/oldfile.pdf I get an HTTP 403 (Forbidden) message. I assume this is because I’m not entitled to open directories from my Web browser, though I am entitled to open the files they contain. Is there any way round this?

    For information, there’s nothing wrong with my index.php file: http://www.example.com/oldfile.pdf/index.php works fine.

  4. by admin

    On May 6, 2011 at 4:27 pm

    Hi Ed,

    What kind of server are you on?

  5. by jaffa

    On August 1, 2011 at 7:11 am

    Hi

    I’m trying to redirect a pdf which is stored in a uploads folder on my server. I would like to redirect it to a different pdf stored on the same server but in a different location.

    Is that possible?

    I am using wordpress, if that makes any difference.

    Hope you can help as I’m quite confused now :(

  6. by Rick Kalkowski

    On November 30, 2011 at 1:51 pm

    Hello, I’m trying to use php to do a pdf to pdf redirect and I can’t seem to make it work. I’m confused as to why the location below isn’t http://www.example.com/filename2.pdf?

    How does ‘new-page-to-redirect-to.php’ fit in? (obviously, this isn’t my area of expertise!)

    I’m trying to redirect:

    http://www.pacaero.com/pdf/resourcecenter/White%20Paper%20-%20Explosive%20Welding%20AWS.pdf/

    to

    http://www.pacaero.com/pdf/resourcecenter/whitepaper-explosive-welding-aws.pdf

  7. by Anna E

    On March 8, 2012 at 1:29 pm

    Have any .aspx sample code we can grab? Thx ~Anna

7 Responses to “How to Redirect PDF Files with PHP, ASP or .htaccess for SEO”




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

By submitting a comment here you grant Streamline Metrics a perpetual license to reproduce your words and name/web site in attribution. Inappropriate comments will be removed at admin's discretion.