I am looking to host a ".exe" file for our clients to download and i wanted to place it at "www.example.com/file". i have created a .htaccess route from "/file" to the location of the program but when you go there the browser doesnt know what file extension to use.
Is it possible to slip the file extension in there?
The simplest way would be to use .htaccess to forward the browser to the .exe file.
RewriteRule /file /file.exe [R,L]
If you really want to keep it hidden for some reason then you'd have to write a script to output a Content-disposition header:
Content-disposition: attachment; filename=file.exe
You need to set the mimetype, and then, it depends on the browser if it changes the extension or not. Some developers use an iframe with the file, with extenstion, to hide the ugly url.
There is a big change apache already sets the mimetype.
According to this site, the mimetype should be one of the following:
application/octet-stream
application/x-msdownload
application/exe
application/x-exe
application/dos-exe
vms/exe
application/x-winexe
application/msdos-windows
application/x-msdos-program
Related
In my HTML file there are several <img src="images/<filename>.jpeg">
The directory "images" holds these files:
<filename>.jpeg
as well as
<filename>.webp
and
<filename>.jpeg.webp
The latter two are identical webp versions of the jpeg file.
Now I want to configure Apache 2.4 on Oracle Linux 8.6 for 'content negotiation'. I am expecting that Apache returns a .webp file instead of the requested .jpeg file, if the browser supports .webp. I don't want to use the HTML <picture> tag or 'srcset' for several reasons, but leave the code untouched.
I have found several promissing configuration examples for nginx, but unfortunatly only litte on Apache:
https://gist.github.com/sergejmueller/5500879
https://stackoverflow.com/a/58857260/4335480
These two links outline 'rewrites' that are to go to the .htaccess file in the /images directory. I tried them both as '.htaccess' in the 'image' directory and it didn't work. I also put them directly in the httpd.conf and it didn't work either. And I tried these lines in the root directory's .htaccess
'AllowOverride All' is included in all section. Even the 'images' directory is explicitly listed.
In Chrome Dev Tools I verified that the request headers include 'image/webp'.
Probably not necessary: In my despair I have disabled nosniff on the Apache server and verified in the response header that it isn't set.
Whatever I try, the server only returns the jpeg file. I can verify this not only by the file name but also by the content-length field in the response header.
So what can I do to have Apache serve avif, webp and (fall back) jpeg in that order, whenever a jpeg file is requested?
Found the error myself. Note to self: don't just copy code snippets to use them. Read and understand them to find errors or identify necessary adaptions.
Vincent Orback's code is often cited for this problem, so I blindly trusted and used it: https://github.com/vincentorback/WebP-images-with-htaccess
It contains the following line:
RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
The outcome is that .webp images are only searched for in the web server root directory. On my site, images are in a subdirectory called 'images'.
Trying to load an image in the browser would fail (deliver the jpeg, not the webp version):
https://<my domain>/images/<image name>.jpeg
But after altering above line to
RewriteCond %{DOCUMENT_ROOT}/images/$1.webp -f
eventually everything worked!
All the other things were unnecessary. You only need one AllowOverride All before the virtual host containers for <Directory / > and all servers and subdirectories would have .htaccess enabled, if present. For this problem, only one .htaccess in the image subdir was necessary, none in the root and no special httpd.conf entries. I turned nosniff on again. The alternativ .webp files just need the extention .webp, not .jpeg.webp
I'd like to let people access files on my root domain directory without having to specify the file extension.
So, for example, there is currently a z9.html that a browser can access with www.mysite.com/z9.html. I would like to let people put in www.mysite.com/z9 to get the file.
The pecking order would be to look for a file of the name submitted with a .php extension, and then, if none found, look for a file of that name with a .html extension.
I don't know why my question was downvoted. It seems like a perfectly reasonable question for the Apache group. The answer is to use Apache's mod_rewrite:
To map any filename without an extension to that filename + .html place the following in .htaccess:
RewriteRule ^/([^.]+)$ /$1.html [L]
Let's say you have the following setup.
You have a server with the basepath set to the normal webroot.
The server has files in the following structure
/projects
/some-unique-id
/index.html
/images
/some-unique-id
/index.html
/images
Is it possible to have a .htaccess file somehow redirect the paths so if index.html has absolute paths they work.
For example if index.html contains /images/foo.gif can this magically become /projects/some-unique-id/images/foo.gif
The equivalent of the base html tag.
This is part of a CMS deliverable previewing system so I am restricted to not changing the HTML code.
Is there anyway to address this in an .htaccess file?
If each index.html contains <img src="/images/foo.gif"/>, the browser will request that URL and there will be no way for the server to know which page caused the request since the requests will be identical regardless of the originating page.
There is no way to do this with mod_rewrite unless you try to check the Referer header, which would be unreliable at best.
You will have to change your HTML markup to solve this. Using relative URLs seems like it would solve your problem.
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?
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
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.
I'm trying to host a python script using an apache web server, but the server tries to run the script instead of just offering it for download.
I do not have direct access to server, and adding the line
AddType text/plain .py
to .htaccess in the root folder does not appear to work, though I could be doing something wrong.
How do I get the server to just send the file as text instead of trying to run it?
-Edit
Changing the name does not work. Script.py.safe still give a 500 Server error when you click it.
I should also mention that the .htaccess file does work, but for some reason that one addType line is not working. Either because it's not overriding something, or the line is wrong.
In your .htaccess:
RemoveHandler .py
If you can't change the Apache config and you can't override it with an htaccess file, then it seems to me that the easiest solutions would be either to change the file extension, or else to write a script that prints the contents of the target script.
Both are hacks to some extent, but the correct solution is to change the Apache config.
One option is to change the extention and make clear that it should be renamed. IE python.py.safe or python.py.dl. The user would then need to remove the extra bit.
You could also Zip it up.
I would write something that loads the python script up. This way you could even get energetic and include formatting and styling of the code. You could even write it in python since you will not have precluded that by file extension.
<IfModule mime_module>
<Files *.py>
ForceType text/plain
</Files>
</IfModule>
in a .htaccess for the folder should work ;)