Forum Moderators: phranque

Message Too Old, No Replies

rewrite .pdf to .html

         

activeco

1:29 pm on Aug 6, 2005 (gmt 0)

10+ Year Member



Is it possible to redirect all requests for *.pdf files to new *.html files, all in the same directory.
Can it cause some problems?

fish_eye

1:50 pm on Aug 6, 2005 (gmt 0)

10+ Year Member



As far as I know there should be no problem at all

RewriteRule ^([^.]+)\.pdf$ http://www.example.com/$1.html [R=301,L]

(A hacked version of something Jim gave me just a few days ago).

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).

activeco

1:55 pm on Aug 6, 2005 (gmt 0)

10+ Year Member



That was fast!
Thanks fish_eye (and thanks Jim too ;)).

fish_eye

2:06 pm on Aug 6, 2005 (gmt 0)

10+ Year Member



Make sure you blame me if it's wrong - I have hacked it. :)

fish_eye

2:22 pm on Aug 6, 2005 (gmt 0)

10+ Year Member



Now I look at it....

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"?

jdMorgan

2:48 pm on Aug 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



activeco,

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

activeco

3:49 pm on Aug 6, 2005 (gmt 0)

10+ Year Member



It works perfectly.

I guess plugin is triggered by document source and not by extension, so everything seems OK.