Forum Moderators: phranque
RewriteRule ^([^.]+)\.pdf$ http://www.example.com/$1.html [R=301,L]
This will redirect ALL pdfs in the directory where the .htaccess is placed and all sub-directories too (so you place this as a separate file rather than as a line in your main file).
What does this mean anyway?
([^.]+)
. = is anything
[^.] = None of that char
+ = 1 or N of the preceding text (N > 1)
Therefore [^.]+ means 1 or more of the preceding thing that can't be anything.....
But seriously, is the '.' in this case literally a character '.' or is it the "Any single character"?
Since pdf files are opened with Adobe Acrobat Reader, and .html files are opened in the browser, I'd suggest testing this carefully before deploying it on a live site. Test it with browsers configured to use a plugin and also with browsers configured to open pdfs externally using the Acrobat reader application.
fish_eye,
"[^.]+" is "One or more characters, not equal to a literal period." In other words, match everything up to the period that delimits the filetype. The escaping rules within [] differ slightly from those generally applied. I have referred to this type of pattern before as "a forward-looking negative match." I use it whenever possible, because it is more specific and far faster than using "(.*)" in patterns like ^(.*)\.<anything>$
You will also notice that it is used widely in the Apache URL Rewriting Guide cited in our forum charter.
Jim