Getting internal error 500 instead of 404 Apache rewrite - apache

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

Related

Rewrite rule to return status 200 for certain URLs

I want URLs with a specific path to return an automatic 200 status response. I have tried the below, but get a an error when I try to start Apache:
First error: RewriteCond: bad flag delimiters
RewriteEngine On
RewriteCond %{THE_REQUEST} GET /the_url/
RewriteRule ^ - [R=200]
If I remove the path part then I do not get the error:
RewriteEngine On
RewriteCond %{THE_REQUEST} GET
RewriteRule ^ - [R=200]
But, of course, I need a way to include the path requirement.
Another error is that, even when the server does return a status 200 above (verified in Developer Tools), then the page still displays an error message: "OK The server encountered an internal error or misconfiguration and was unable to complete your request...." Is it not really returning a status 200? Or is it, but this is just what the default HTML page is when nothing is provided by the server?
The following did the trick. From https://httpd.apache.org/docs/2.4/custom-error.html
Enable the mod_rewrite module
LoadModule rewrite_module modules/mod_rewrite.so
and then add the following to your virtual host.
ErrorDocument 200 "ok"
RewriteEngine On
RewriteRule "/the_url/" - [R=200]
EDIT:
My original answer has been edited so much by someone else that it is no longer quite the same. I'll leave the edited answer (above), but I did not have to enable the mod_rewrite module. Some Linux distributions come with this by default or your host might have provided it as their default starting point. I added the three lines to httpd.conf.

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)

htaccess errordocument not working

My understanding is that to show a custom error page, one must put
ErrorDocument 404 /error/404.php
in .htaccess
Also,
AllowOverride All
must be set in httpd.conf file. However, right now it's set to
AllowOverride None
If I change None to All, then restart the server, I get a Internal Server Error 500 when I deliberately enter gibberish in the address bar. What am I doing wrong?
Other things in the file
IndexIgnore *
followed by
RewriteEngine on
followed by rewrite rules.
RewriteEngine on is the cause of the problem--see below.
Short answer: This error means you forgot to enable the rewrite module and is not related to ErrorDocument.
How to fix it?
Make sure you have enabled the rewrite module. This can be done by doing one of the following:
Uncommenting LoadModule rewrite_module modules/mod_rewrite.so in your Apache configuration file.
Run a2enmod rewrite as root (only available on Debian-based systems).
Why are you only getting this error after you added ErrorDocument?
AllowOverride was set to None, which means your .htaccess will be completely ignored. Later, you set it to All to enable .htaccess files.
However, you forgot to enable the rewrite module but you tried to use it in your .htaccess. This is not related to ErrorDocument and is simply a coincidence. The following error you posted is clearly showing it:
[Sat Aug 17 17:33:46 2013] [alert] [client 127.0.0.1] C:/Data/MyWeb/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

Apache not processing encoded URLs with %3F

The problem url links to my website are of the form
/fullpage.php%3F%20cp3_Hex%3D0F0200%26cp2_Hex%3D000000%26cp1_Hex%3DFC2024
The un-encoded url is
/fullpadge.php?cp3_Hex=0F0200&cp2_Hex=000000&cp1_Hex=FC2024
Apache returns a:
403: You don't have permission to access /fullpage.php? cp3_Hex=0F0200&cp2_Hex=000000
I have tried the following rewrite rule
RewriteRule ^/fullpage.php%3F(.*)$ /fullpage.php?$1
to no avail
Any ideas
You are almost certainly getting a 403 error.
The error is caused because ? is a banned file/directory name character on Windows and Linux. This means when Apache attempts to find a file or directory named "/document/root/index.php?blah" (after decoding) and it causes a 403 error. This is before the .htaccess files are read so you cannot use mod_rewrite in the .htaccess file to override this 403 error or an ErrorDocument defined in the .htaccess file to catch this error.
The only way to catch %3f is to use mod_rewrite or an ErrorDocument in a "VirtualHost" e.g. in httpd-vhosts.conf (or the main server configuration if there aren't any "Virtualhost"s e.g. in httpd.conf).

Custom errors in apache config files (not error pages)

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.