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

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.

Related

Rewrite rules in Apache

Can anybody tell me where to put Rewrite rules in httpd config file and tell me also how can I write rules in different file and include that file in httpd config file
Rewrite rules in a separate file go in the hidden .htaccess file. This file goes in the public webserver's folder where you want the rules to apply and it is applied in a way that it respects .htaccess files on higher folder hierarchy.
For .htaccess files to be considered you need to add AllowOverride All in the Directory Tag where your server is configured.

Can't remove index.php without 404 error

I'm using Joomla 2.5 and Apache and I have followed this steps:
1- mod_rewrite module is eneabled? YES
2- htaccess.txt renamed to .htaccess
3- set "Use URL Rewriting" to YES.
And this is what I get:
Not Found
The requested URL /about-us was not found on this server.
The web is located in /var/www/
The .htaccess is located in /var/www/
And this is my .htaccess: http://pastebin.com/dq1TYs1t
Thanks for the help.
Since you said allowoverride was set to none, your .htaccess file will be ignored. You need to set allowoverride to all the other option is leave allowoverride at none, and take the contents of the .htaccess file and incorporate it into your apache configuration file. This has the benefit of being slightly faster as apache doesn't need to look in directory tree for .htaccess files (they are really good to allow users that don't have access to the configs the ability to override the base settings, but if you have access to /var/www you should also have access to make changes to the config files.
There is no need to enable any mod_rewrite module.
Need to enable URL rewriting option in global configuration. Also need to rename htaccess.txt file to .htaccess.
please check there is no any third party component of security like admin tools are enable or installed which is blocking this mod rewrite option.

disable lookup for .htaccess file in subdirectories via .htaccess in root

Is it possible to disable lookup for .htaccess file in subdirectories, when I know I will only need my .htaccess in root directory?
I know it can be done with "AllowOverride None" when you have access to the server configuration file. But can I do this with my .htaccess file in root as well?
No. It must be done inside a <Directory> directive, which can only exist inside core configuration files.

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.

Set path to php.ini

Is it possible to have just a single php.ini file, for example in the webroot (or even outside of it to prevent people accessing it via GET), and tell PHP quickly and easily where it is?
I know you can set php.ini directives in .htaccess, but is it possible to define a specific php.ini file to be used?
Add this to your server configuration...
<VirtualHost 1.2.3.4:80>
PHPINIDir /path/to/new/php_ini
</VirtualHost>
Make sure to just include the path to the directory, not the entire path to the file.
Then restart Apache.
Check it worked with phpinfo().
Have a look at .user.ini section at the php docs.
Since PHP 5.3.0, PHP includes support for .htaccess-style INI files on
a per-directory basis.
But beside the .unser.ini solution you can place an additional ini file in the "additional .ini files parsed" directory. There you can use one single ini file to overwrite all other settings. Name it with zzz at the beginning and it will be parsed at last. This is also easy for your hoster to deploy without destroying his settings.
Kolink, I suspect that you are on a shared hosting service, in which case your host may be using something called suPHP. In this case -- as you describe -- the PHPINIDir directive doesn't work, in which case there is a suPHP_ConfigPath directive.
In terms of access, I have a standard mod_rewrite in my DOCROOT/.htaccess:
RewriteEngine On
RewriteBase /
# if a forbidden directory or file name (starting with a . or /) then raise 404 Fatal
RewriteRule (^|/)[_.] - [F]
What this does is forbid any request for any filename or directory prefixed by . or _. I have a DOCROOT/_private where I keep this stuff for me:
suPHP_ConfigPath DOCROOT/_private
where you will need to replace DOCROOT by your local setting on your service. Look for DOCUMENT_ROOT in a phpinfo() listing.