Tuesday, 27 August 2013

.htaccess redirect broken links to php page with parameters intact

.htaccess redirect broken links to php page with parameters intact

I have a php website with a docs folder in the root. The structure of the
site is thus sitename.com/docs/. In the docs folder are PDF's that
sometimes cause browser issues because of spaces in the names, so they are
not found (mainly by IE). What I would like to do is the following:
Whenever there is a broken url in the docs/ directory, I would like to
redirect the entire query string to a php page within the docs directory,
but it must keep the name of the pdf intact. Thus, is the url is
website.com/docs/this is a pdf, I want it to redirect to
website.com/docs/index.php?pdf=this is a pdf. From there, I can grab the
pdf param and fix it up and send the request to the correct file.
The reason this is not done with straight .htaccess is that I cannot find
a solution that is dynamic - in other words the number of words in the PDF
is variable, and could be from 1 to 20 words, separated by spaces. I had a
post up here about that at this SO post which did get one reply, however,
it still does not address the variable url length problem.
I have again tried this from the examples in this tutorial but this has
not helped me at all as I cannot fathom how to do this properly.
The one thing that I think is close is the following code:
RewriteEngine on
RewriteRule ^docs/(.*) /index.php?pdf=$1 [NC]
Am I close?

No comments:

Post a Comment