Thursday 26 July 2012

Redirect PDF With '%20' Using .htaccess

I'm so glad I sorted the re-directing of PDF files with '%20' in them and using the .htaccess file.

It seems a common occurrence that when PDF files are created the file name uses spaces, just like you'd save a word document on your computer, but the difference being a word document very seldom gets used as a document on a website, whereas a PDF does.


Why is this a problem, well it isn't until the PDF (when it is beoing used on a website) URL gets renamed and you need to redirect it to its new URL; i.e. someone has eventually seen (within the company) that the PDF isn't using normal URL directives, meaning the URL probably contains capital letters and spaces, similar to this:

/downloads/How%20to%20Change%20URLS.pdf

As you can see we've plenty of '%20' within the URL, this is the correct procedure for accommodating the spaces in a URL, fine - well not really because if you were to redirect this URL in the .htaccess file using:

redirect 301  /downloads/How%20to%20Change%20URLS.pdf

Well, you'd get a 500 server error and your website will go down. How do you then redirect this type of URL without any hiccups?

Easy.

Wrap the URL to be redirected in "" that's quotes, so for example:

redirect 301 "/downloads/How%20to%20Change%20URLS.pdf"

To make that totally clear and lets say the new destination is:

http://www.mywebsite.com/how-to-change-urls.pdf

The line of code in the .htaccess file would be:

redirect 301 "/downloads/How%20to%20Change%20URLS.pdf" http://www.mywebsite.com/how-to-change-urls.pdf

Done.

2 comments:

  1. Update to this post:

    If the URL isn't a 'PDF', it seems having the '%20' in string stops the redirect from working, therefore this is what you need to if say the page is '.html':

    redirect 301 "/downloads/How to Change URLS.html http://www.mywebsite.com/how-to-change-urls.html

    You can see all I've done is replace the '%20' with a space.

    Again Done...

    ReplyDelete
  2. My brother suggested I might like this website.
    He was entirely right. This post truly made my day. You can not imagine simply how much time I had spent for this info!
    Thanks!
    My web site uk seo

    ReplyDelete