mod_rewrite vs symlink: the browser is not fooled
-
06-09-2019 - |
Question
I have a file foo.bar.1 on my server and when I try to access it from a browser (firefox) I get a popup that says "You have chosen to open foo.bar.1 which is a: 1 file ... What should Firefox do with this file...". If I create a symlink to it, foo.dat, I can access it just fine; the contents display in the browser as I expect.
My problem is that I don't want to create symlinks for all these files, I want to use a mod_rewrite rule, like
RewriteRule ^([^/]+)\.dat$ $1.bar.1
But that doesn't act like the symlink. It gives the same popup (though, strangely, it now says "You have chosen to open foo.dat which is a: DAT file ...".
How can I do the rewrite rule such that the browser is tricked into treating it like a normal .dat file, as is accomplished by the symlink?
Solution
If you have mod_mime installed, then you can do:
AddType text/plain .1
Not tested, but it should display the file instead of download
See http://httpd.apache.org/docs/2.0/mod/mod_mime.html#addtype for AddType documentation
OTHER TIPS
Look in to the [T]
argument to the RewriteRule
directive:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
When you make the symlink, Apache thinks it's serving a .dat
file, whereas when you use mod_rewrite, Apache still thinks it's serving a .1
file. This turns up in the Content-type header served to the browser.