FilesMatch or Files for Single File HTACCESS - apache

My site has subdirectories that include index.html files.
www.domain.com/.htaccess
www.domain.com/index.html
www.domain.com/otherstuff/index.html
I have the following in the .htaccess file to run some php code on my main index page, but I know it also affects the index.html files in my subdirectories. How do I write it so it applies only to www.domain.com/index.html?
<Files index.html>
AddHandler x-httpd-php5-cgi .html
</Files>
I found this:
Precise targeting of the Filesmatch directive
But I'm not interested in using an alternative method like redirecting because it doesn't answer the question. I want to know if it's possible to do it this way and if it is, how? If it's not actually possible, then I'll know that for future reference and will always use an alternative.
Thanks!

See the RemoveHandler docs:
The RemoveHandler directive removes any handler associations for files with the given extensions. This allows .htaccess files in subdirectories to undo any associations inherited from parent directories or the server config files.
So in the "otherstuff" directory, add another htaccess file that says:
<Files index.html>
RemoveHandler .html
</Files>

Related

.htaccess to block specific file

I been searching and couldn't find this answer, usually they all ask how to block viewing the file from outside the domain.
I have a website where I don't have access to the remove a script by hand or Javascript since it executes before I can do it, but I do have access to the folder, so I was thinking if I can add an .htaccess blocking that specific JavaScript file (not all the .js files, only one which is prototype).
Is there any code to do to that?
Thank you in advance.
Yes there is! Put this in your .htaccess file:
<Files "example.js">
Order Allow,Deny
Deny from all
</Files>

Shutting down a website using HTaccess does not work

OK, it's very simple but it does not work. I have a wiki site where the root contains an index.php file and the subdirectories contains the content of the wiki (I use PMwiki, so no database is required)
I want to temporarity shutdown the website and make it unaccessible by using an nice HTML page to display the shutdown message. I could rename the index.php file, but the rest of the files in the subfolder will remain accessible.
The first thing that worked but which is not elegant is restricting the whole site with a password in the htaccess using "Require valid-user" and all it's other command. The problem is that I cannot display a nice shutdown message as an HTML file.
Else I tried renaming the index.php file to something else like site.php. Creating a index.html file as a message and using a script like this:
Order Deny, allow
Deny from all
<File "index.html">
Allow from all
</File>
In that case, the index.html file is accessible, but it must be manually typed in the URL, it will not use this file by default. I tried adding DirectoryIndex directive like this
DirectoryIndex index.html
But it still does not work.
So first is there a way to make the user only see 1 page in particular and block everything else.
Second, doing so makes the site unaccessible to me. So is there a way to passords restrict the whole directory structure except for a specific index.html file. So that I could type url/site.php and be able to enter my website using an htaccess password.
Thanks for any help
Just this rule in root .htaccess should be able to handle this:
RewriteEngine On
RewriteBase /
RewriteRule !^shutdown\.html$ shutdown.html [L,NC]
Now you can keep custom HTML content in /shutdown.html. Keep in mind you need to use inline css/js since it will also rewrite css/js requests to /shutdown.html file.

Is it true that httpd looks for .htaccess files in all higher-level directories?

Given the directory www/html/file.php would it be it be appropriate to place my .htaccess alongside with file.php?
That way making rules for file.php (demo example below)
~Rule~ file.php ...
file.php would be located.
No. It depends on the setting of AllowOverride for specific directories - however, in most configurations AllowOverride is enabled for the document root.
See http://httpd.apache.org/docs/current/de/howto/htaccess.html#page-header
According to that documentation, you should put any rules into the global configuration file instead of .htaccess files if possible. if you can't access the global configuration file, you should put the .htaccess file into the folder it applies to.

Starting and accessing sessions using auto-prepend-file from .htaccess

I have been trying to use the 'auto-prepend-file' value to set a PHP script to be ran before every page from that directory. Currently, I'm destroying and creating a session, then setting a session variable.
But if I try to access session variables from a page, there is no value in them.
Can this value be prevented from being set in a .htaccess file?
Will the prepended script be ran when called for non-php pages aswell?
Can this value be prevented from being set in a .htaccess file?
It is possible to disable session cookies with a .htaccess file, but I doubt that's the real problem in your case. Are you sure the file is actually getting prepended at all? Try a more direct test, like adding die('The prepended file was executed.') to the file.
Will the prepended script be ran when called for non-php pages aswell?
The auto_prepend_file directive only applies to files parsed by PHP. In most server configurations that will only include .php files. However, you can use the AddHandler directive to make Apache execute PHP in other file types as well.
For example, if you use AddHandler to add .html as another file type that can contain PHP code, auto_prepend_file will also apply to .html files.
What ended up solving it for me was setting the "AllowOverride" directive on my Apache configuration file. In order to allow .htaccess privileges on given folder, you should have something like...
# Allow .htaccess settings
<Directory "/absolute/path/to/htaccessfolder">
AllowOverride Options
</Directory>
...on httpd.conf, which allows overriding option settings from the selected directory.

Excluding one directory in .htaccess (not just rewrite rules)

Excluding one or more directories from rewrite rules in .htaccess files seems to be a common question. However, my .htaccess does more than just set rewrite rules. I've also set some server changes (we don't have suPHP on this server) as well as set some prepending of some php files. For example these are a few examples:
# Make files ending in .php, .html and .xml files etc. parsed by php.
AddType application/x-httpd-php .php .html .xml .css .js .le .txt
<FilesMatch "\.html$">
php_value auto_prepend_file "/home/2427/spwebsites/www.spwebsites.co.uk/incs/phps/config.php"
</FilesMatch>
# Internal Server Error
ErrorDocument 500 /admin/errors.html?code=500
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)/$ $1.html [L]
I don't want any of these set for one directory (where my word press installation is), is there a way I can do this? Can I set a conditional statement for the whole .htaccess file?
Adding a blank .htaccess file in the word press directory won't work because this won't undo the settings in the parent directory.
I was just looking into your dilemma and it is a tricky one. It would be nice to be able to have the DirectoryMatch directive available in .htaccess ...
What you can try is to reset your values in the specific directory via another .htaccess file.
So in the case of the AddType perhaps, resetting it back to just ".php" might work (assuming it doesn't inherit the other values). Definitely not an ideal solution with out access to the main config file/ virtual host.
Here is a weird idea that you can try/test ... place the "wordpress" dir outside of the main root (or whereever you have the offending .htaccess file). Now route all requests to the wordpress (inner dir) to the outer dir. I wonder if Apache would not use the offending .htaccess considering the requests are being routed?