0

I'm trying to clean up our website. I'm using ISAPI Rewrite. I have figured out the regex I need to select the files in question.

^(?!.*?web_content.*?).*?\.pdf

I want to redirect all pdf requests to look in web_content/pdf/. So The pseudo rule I want is

redirect all requests to pdfs that aren't already being requested from web_content/pdf. Drop off the original folder path. /somefolder/this/mycool.pdf ==> /web_content/pdf/mycool.pdf

My question is how would I actually create the Mod rewrite rule? I don't know how to correctly do the replacement command. I also hope this rule wont affect external pdf links on our site.

Doug Chamberlain
  • 11,192
  • 9
  • 51
  • 91

1 Answers1

1

This should work:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/web_content/pdf/
RewriteRule ^(.+\.pdf)$ /web_content/pdf/$1 [L]

Helicon ISAPI_Rewrite v3 is pretty much the same as Apache's mod_rewrite (except few advanced things), therefore almost all rules that work on Apache will work here as well.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • Thanks so much, this should fix my problem. Is there a good clear reference that I can read up on this? Apache's ref is hard for me to understand. especially using back references between Rules and Conditions. – Doug Chamberlain Sep 02 '11 at 12:28
  • Good question -- but I personally do not know any -- I have learned all this myself by implementing rewrites on real site by reading ISAPI_Rewrite docs and following their examples ( http://www.helicontech.com/isapi_rewrite/doc/examples.htm ). The rest "slowly" comes with experience after modifying and creating more sophisticated rules and working with regex in general. I've used only official docs and some random articles full of recipes/examples found after googling. – LazyOne Sep 02 '11 at 12:41
  • The processing order flow chart is what is really confusing me. Really Apache? Really?. http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html – Doug Chamberlain Sep 02 '11 at 12:44
  • It seems what you have is exactly correct. I would expect to be able to use line anchors. However, the rule doesn't work as you have listed it. – Doug Chamberlain Sep 02 '11 at 12:51
  • What exactly does not work? It works fine for me on my dev box (I'm not having pdf files but mp3, and folder names are different, but that is the only difference). – LazyOne Sep 02 '11 at 13:01
  • This is what does work, I am using a .htaccess file, if that makes a difference: `RewriteCond %{REQUEST_URI} !/web_content/pdf/` `RewriteRule ^.*?([^/]+\.pdf)$ /web_content/pdf/$1 [L]` – Doug Chamberlain Sep 02 '11 at 13:12
  • I see -- you taking file name only and ignoring any folders. Well -- there was no such requirement in your question, so i wrote my rule to take relative path to pdf file and preserve it: e.g. `/somefolder/file.pdf` will become `/web_content/pdf/somefolder/file.pdf`. With your approach you will have problems if you have more than 1 file with the same name but in different folders. – LazyOne Sep 02 '11 at 13:34
  • You are right, I updated the question description. Thanks for your help. – Doug Chamberlain Sep 02 '11 at 14:57