Custom errors in apache config files (not error pages) - apache

I'm not looking to create custom error pages, but to actually issue error messages when the config file is parsed. Specifically, I want to use an <IfModule> to throw up an error if the module hasn't been loaded, so it's easier to debug.

This is the only kludgy thing I could think of (and requires mod_rewrite enabled):
<IfModule !mod_deflate.c>
ErrorDocument 500 "mod_deflate isn't available"
RewriteEngine On
RewriteRule .* - [R=500]
</IfModule>
If you find a better way to 'trigger' errors, I'm certainly interested ;)

For Httpd 2.4 see http://httpd.apache.org/docs/2.4/en/mod/core.html#error
For older versions
<IfModule !mod_deflate.c>
Mod_deflate_not_enabled.
</IfModule>
will raise something like
Syntax error on line 7 of /etc/apache2/sites-enabled/000-default:
Invalid command 'Mod_deflate_not_enabled.', perhaps misspelled ...
...fail!
when you reload the server config.

Related

Getting internal error 500 instead of 404 Apache rewrite

I wrote this in my .htaccess file inside my include folder but instead of getting a 404 error message I am getting a 500 internal server error message. How do I change this to a 404 error message?
RewriteEngine On
RewriteRule ^includes(/|/.+)?$ - [R=404,L]
You probably don't have mod_rewrite turned on. Make sure the rewrite module is loaded in your server config, should be a line that looks something like:
LoadModule rewrite_module modules/mod_rewrite.so
or something similar (uncommented). Also see: .htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

Apache Error directive ignoring ErrorDocument directive

I wrote on my .htaccess an if to check if the *mod_rewrite* is installed and throw error 500 if not.
ErrorDocument 500 /500.html
<IfModule !mod_rewrite.c>
Error "mod_rewrite not installed"
</IfModule>
If i get an 500 error by it will show the 500.html. But if it gets that same error from the Error directive, it will show the default error page instead. Why ? How do i fix it ?
Well, after thinking a lot about it and reading a bit, i believe that the Error function simply makes the .htaccess parsing to be cancelled - and the error to be logged - and then throw an 500 error from the apache config that has been parsed before this one (for example, an .htaccess from another folder over this one or the httpd.conf from apache)

Apache caching urls, RewriteEngine causing problems

I was trying out the Apache Rewrite Engine, but was having problems because it cached it's responses. Now if I change the file, it does not do what the file says, but does what it did when I tried the same url before.
For example, I tried to type in
localhost/api
but I had not yet set a rule that would match that. The server gave me a 404 error. I tried to add a rule that would work with this url:
RewriteRule api/? api.php [L]
but it still gave me the same error. If I try an address like localhost/lapi which I didn't enter before I added the rule, it works. Is there a way to clear this cache and restart? I tried restarting apache using apachectl -k restart and apachectl -k graceful but I still had the same problem. Does anyone know how to fix this problem?
First of all, depending on the context of your rule, you may need a leading slash as in:
RewriteRule api/? /api.php [L]
Regarding the caching, do you have mod_cache enabled? If so, disable it and restart your server. mod_rewrite does not do any caching on its own. Also be sure to completely clear your browser cache and be aware that if you are not accessing your server over the same local network, there may be other caching taking place along the way.
This problem can also be caused by the MultiViews option, see Apache doc. Try omitting Option MultiViews from httpd.conf or add Options -MultiViews to .htaccess.
(The MultiViews option enables Apache to choose automaticaly between several versions of the file based on the file name extension and content preferences present in the HTTP request (i.e. something you probably don't want to happen). It can lead to this kind of bizzare failure when a rewrite rule just removes the script's extension.)

How to enable ErrorDocument for 400 error?

I have installed apache2 on my ubuntu machine. As I need to work with subdomains I created a proper entry in sites-available which looks like this:
<VirtualHost *:80>
ServerName xxx.localhost
DocumentRoot /var/www/
</VirtualHost>
I also enabled mod_rewrite (and changed "AllowOverride All" in my sites-available/default file) but other than that nothing else was changed.
My .htaccess file does work, and I wanted to handle some error codes. Doing so with 404 worked pretty well, but for some reason other errors don't seem to work. I'm mostly interested in handling error 400:
ErrorDocument 400 /400.php
ErrorDocument 404 /404.php
Is there anything else I should look at? I couldn't seem to find any place where 404 are allowed while other error codes aren't.
If the php is returning the 400 error, then php should generate the error document.
Use something like:
if( $someError )
{
http_response_code(400);
include("{$_SERVER['DOCUMENT_ROOT']}/400.php");
exit();
}
From the Apache documentation:
Although most error messages can be overriden, there are certain circumstances where the internal messages are used regardless of the setting of ErrorDocument. In particular, if a malformed request is detected, normal request processing will be immediately halted and the internal error message returned. This is necessary to guard against security problems caused by bad requests.
Try adding it directly in the httpd.conf and restart Apache.

Apache mod_speling case insensitive URLs issue

I want to have case insensitive URLs using Apache's mod_speling module, but this is producing unwanted lists of "multiple options" whilst the Apache documention says
When set, this directive limits the action of the spelling correction to lower/upper case changes. Other potential corrections are not performed.
I'm testing this on an Apache 2.2.16 Unix fresh install but I'm still running into exact the same problems as submitted in 2008.
It's unexpected (and not wanted) behaviour when Apache lists a few "multiple choices" (status code 300) when the checkCaseOnly directive is on!
I have this in my httpd.conf:
CheckSpelling on
CheckCaseOnly on
First directive to use the mod_speling, second directive to limit only to case corrections
What am I doing wrong?
TLDR: CheckCaseOnly is broken due to a bug that has remained unfixed for over six years as of 10/2014.
I know this is an old question, but I just ran into the same issue. This update is to help others with the same issue.
The current answers to this question are incorrect, as the OP is using mod_speling correctly, but there is a bug.
https://issues.apache.org/bugzilla/show_bug.cgi?id=44221
The underlying issue is that the apache people are in disagreement over fixing this behavior because it changes the rest of the module. This has remained unfixed for something like 6 years.
To enable mod_speling (either by Location or VirtualHost) use the directive:
CheckSpelling On
If all you want is case insensitivity use:
CheckCaseOnly On
You also need to have RewriteEngine enabled:
RewriteEngine On
On Ubuntu 12.04 LTS using Apache 2.2, I did the following:
Create speling.conf in ${APACHE}/mods-available to provide the config options.
<IfModule mod_speling.c>
CheckSpelling On
CheckCaseOnly On
</IfModule>
Link speling.conf and speling.load into the enabled modules directory ${APACHE}/mods-enabled:
# cd ../mods-enabled
# ln -s ../mods-available/speling.conf speling.conf
# ln -s ../mods-available/speling.load speling.load
Restart the server.
# service restart apache2
After reading user1647075's answer about this being a known Apache bug that's unlikely to be fixed, I decided my best option was to hide the "multiple options" page from the user by updating my Apache config to show the 404 error page for 300 status codes:
ErrorDocument 300 /404.htm
Of course, you can also create a custom error page instead of reusing the 404 error page.
Hope this workaround helps.
Do you really want case insensitive URL?
Why not just force lowercase urls, like this?
RewriteEngine On
RewriteMap lc int:tolower
RewriteRule (.*) ${lc:$1} [R]
Have a look at http://www.issociate.de/board/post/265865/make_URL