Forum Moderators: phranque
All the PDF's located in various directories have now been moved to a single directory /pdfs. I need a rule for my htaccess file that will look for any reference to .pdf not located in /pdfs and redirect the name of that pdf to the same name in the /pdfs directory.
I've searched the site and couldn't found tons, but nothing that I could tell would apply to me. I've been on the computer all day and most likely it was right in front of me and I missed it...
Can anyone help an exhausted writer :)
Thanks!
What have you tried so far?
Jim
Jim, I spent hours trying to figure how to redirect an old asp site with query strings to php and after hours of reading (more than half the similar examples were from you (you are the htaccess guru, no question), but in the end, I found that I had found the right example right off, I just didn't use a NC (or perhaps it was the initial forward slash. I am not an htaccess expert, but I am now seriously thinking about buying a book and putting issues like this to rest forever!
I had this which didn't work:
#Redirect 301 /services/departments/department.asp?ID=2 [website.com...]
and ended up with this, which did.
RewriteCond %{QUERY_STRING} ^ID=2$ [NC]
RewriteRule ^services/departments/department\.asp$ [website.com...] [R=301,NC,L]
So, after all that, I didn't feel confident enough to try something more complex and thought I would ask for help.
g1smd, no there is not.
Thanks again to both of you for looking into this!
Jim's almost provided the answer in his detailed two line description just a few posts back. If you copy out all the elements that he listed, you'll have everything you need to put it together.
In our Forum Charter (see link at top of this page), you'll find links to the mod_rewrite documentation and a regular-expressions tutorial. You will also find the reason that the forum contributors and I are often 'indirect' with our answers in threads like this: Our chartered purpose is to help you *learn* to solve your problem, not to write your code for you. There are very few contributors in this forum, and they simply don't have enough time to provide a free code-writing service for the world.
As stated above, you'll need a RewriteCond (look up RewriteCond in the Apache mod_rewrite docs) with a negative match (i.e. use "!" for NOT) on the Request_URI (see list of variables testable by RewriteCond) starting with "/pdfs/" (see regex start-anchor "^").
Then you'll need a RewriteRule (look up RewriteRule) that detects all requests for URL-paths ending with ".pdf" (escape the literal period with a "\" and use the "$" regex end-anchor token).
This RewriteRule needs to recognize any subdirectory path-parts in the originally-requested URL-path and discard them, then capture the filename and filetype for use in the substitution URL.
The RewriteRule should then 301-redirect (use "[R=301,L]" rule flags) to the /pdfs subdirectory and "back-reference" the captured filename and filetype.
Because the most efficient pattern is a little complex and I cannot bring myself to post inefficient patterns, I will illustrate the RewriteRule pattern and substitution:
^([^/]+/)*([^.]+\.pdf)$ http://example.com/pdfs/$2 Note that we "save" the filename and filetype as $2 by enclosing it in the second set of parentheses, and then we "back-reference" that parenthesized sub-expression match as "$2" in the replacement URL.
The first set of parentheses is to allow matching any number (including zero) of subdirectories ahead of the filename, using the greedy "*" quantifier, and we don't back-reference $1 because we want to discard the originally-requested subdirectory path.
So the pattern says, "Match one or more characters not equal to a slash, followed by a slash, and do that zero or more times but as many times as possible. Then match one or more characters not equal to a period, followed by a period, and ending with 'pdf'."
It's actually much harder to write this in words than to write it in code! :)
All you need to do now is to incorporate that pattern and substitution into a RewriteRule, add the rule flags, and add a negative-match RewriteCond as described above to prevent looping.
Jim