Make the server ignoring the underscore in file names when requested by URL - apache

Do you think that there is a trick in htaccess to to make the browser download the file my_file_21.pdf if the user access this one with the path http://example.com/files/myfile21.pdf?
And bonus point if the both paths below :
http://example.com/files/myfile21.pdf
http://example.com/files/my_file_21.pdf
works for the file on the server : my_file_21.pdf

Related

Entering AuthUserFile info in .htaccess when root path is dynamic

I'm trying to follow the instructions on http://www.4webhelp.net/tutorials/misc/htaccess.php in order to create a password file placed outside of the web root and use it in conjunction with .htaccess. The problem I can't solve here is that the AuthUserFile info in .htaccess is supposed to be an absolute path to the server root. I am using a server provided by a web hosting company and I found using <?php echo $_SERVER['DOCUMENT_ROOT']; ?> that the complete path to my web root looks something like /mumbo/jumbo/my_site/public_html where /mumbo/jumbo/ is stuff determined by the web host and, importantly, subject to change without notice. In PHP I can of course generate a path that always is correct using $_SERVER['DOCUMENT_ROOT']. But .htaccess is not written in PHP. Is there a similar way to obtain the server root path upstream of the web root dynamically using the syntax of .htaccess?
From the manual:
File-path is the path to the user file. If it is not absolute, it is
treated as relative to the ServerRoot.
You should give either an absolute path or a relative one to the server root. Neither would solve your problem.

Browse zipfiles on apache webserver

I already have an awk script called viewzip.cgi which works as follows:
...viewzip.cgi/path_to_zipfile/zipfile.zip/
will show the root directory of that file,
...viewzip.cgi/path_to_zipfile/zipfile.zip/subdir/
shows a subdirectory (if present)
...viewzip.cgi/path_to_zipfile/zipfile.zip/path_to_file/file
will download one particular file.
Now what I want is omitting the "viewzip.cgi" part in the URL and an automatic redirect working as follows:
...path_to_zipfile/zipfile.zip
should download the zipfile as it would be standard behaviour, but
...path_to_zipfile/zipfile.zip/
with the trailing slash should redirect to a path like the first example, and also when trailing subdirs or files are appended.
How can I do that, if so? I have access to file system (i.e. ".htaccess") but not to apache's root configuration files. Or is there a (possibly well-known) better solution? A similar problem applies to .chm files which would be more easily browseable when unpacked on server on request. It would be nice if I don't need to repeat a redirection line for each single zipfile I have.
henni
The RedirectMatch keyword does the job.
RedirectMatch .../((?!viewzip\.cgi/).*)\.zip/(.*) http://www.../.../viewzip.cgi/$1.zip/$2

Apache automatically redirects to different file

When a file is not found on my apache server, the server automatically redirects (http 301) to a file with a similar name.
For example if the file
http://myserver.com/files/image012.jpg
is requested, but does not exist, the server redirects to
http://myserver.com/files/image021.jpg
(which does exist).
How can I prevent this?
Your server probably has automatic spelling check activated by default.
Try adding the following line to your .htaccess file
CheckSpelling off

htaccess vs password protected directories

I have to add a password protected zone to a site I am working on (using the .htpasswd file). The thing is the whole directory structure it's being pointed at doesn't exist and is created through mod_rewrite.
Will it still work, or does the directory actually have to physically exist on the server?
Clarification:
While I may need to password protect the directory:
http://sitename/category/protected/
mod_rewrite translates this to:
index.php?category=category&directory=protected
So the actual directory does not exist. Is it still protectable?
You can add the access rules to the apache config file (httpd.conf or similar) in a Directory or Location tag instead of adding it in the .htaccess file.
Your rewrite rules will ultimately point to some files in a directory on your system (unless they redirect users to some external location). The authentication setup should be on the underlying directory that will be accessed.

.htaccess Redirect request to files exts in particular folder only

How do you write rules to redirect all requests to *.php and *.html files in upload/ folder to a text file name forbidden.txt in root www folder. What I'm trying to do exactly is preventing script execution in this dir by redirecting those requests to the text file
Note: The upload/ folder is accessibly by ftp used by a group of people to upload files so I cannot place htaccess inside this folder.
Create an .htaccess file at the root level of your site containing
RedirectMatch ^/upload/.+(html|php)$ http://www.yoursite.com/forbidden.txt
You could also try switching off the PHP engine in that directory by creating an .htaccess file in /upload/ containing:
php_value engine off
although you would need to ensure that people cannot upload files with the name .htaccess
Put your htaccess rules in httpd.conf instead.
If you can't edit httpd.conf, then your best bet is to not allow web access to that directory at all. Let FTP users access a folder outside of your web directory and then provide a mechanism for retrieving the file contents.
You could name that directory "upload". Then you could have your .htaccess file make requests to /upload/myfile execute upload.php, which finds ../upload/myfile and spits backs its contents. This way it would appear to users that they are accessing the "upload" folder directly, but you would the level of control you want through the PHP script.