Force Apache to send 200 instead of 404 in .htaccess file - apache

I need to force Apache to send 200 OK instead of 404 in .htaccess I don't know is this possible. I have found below text from one post but it doesn't work I don't know why...
From: http://www.askapache.com/htaccess/apache-status-code-headers-errordocument.html
Create a blank file in your main web directory named 404.. can be blank.
Add this to your .htaccess file:
Redirect 200 /404
ErrorDocument 404 /404
That will change the Apache ErrorDocument to the /404 file. But the Redirect line causes requests for /404 to issue a 200 OK response instead of a 404.
If that doesn't work it is due to rewrites no doubt. So if that's the case add this under those first 2 lines but above any other rewrite lines in your .htaccess file:
Options FollowSymLinks ExecCGI
RewriteEngine On
RewriteBase /
# If the requested file doesnt exist
# and if the requested file is not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^4]* /404 [L,S=4000]
Explained in more detail at: http://www.askapache.com/htaccess/apache-status-code-headers-errordocument.html#Automate_ErrorDocument_Triggering

In order to send 200 status instead of usual 404 you can use this rewrite rule:
ErrorDocument 404 default
RewriteEngine On
RewriteBase /
RewriteRule ^404\.html/?$ - [L]
# If the requested file doesnt exist
# and if the requested file is not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . 404.html [L]

Related

htaccess error page file is not working with condition of rewrite

I have written a condition to auto add the path in the URL and it is working perfectly but now I want to add error pages but it is giving no response instead giving a 500 error. ErrorDocument with text is working fine but it is not adding file I have already checked all possible paths but still no response.
This is my code in .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /crud/views/pages/$1 [L]
</IfModule>
ErrorDocument 500 /404.html
ErrorDocument 404 /404.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /crud/views/pages/$1 [L]
This will result in a rewrite loop (500 error) if you request a file (or directory) that does not exist when prefixed with /crud/views/pages/.
Note that defining the 500 ErrorDocument in .htaccess is too late for most 500 errors - this ErrorDocument would need to be defined early in the server config to catch this particular error.
You could resolve this rewrite-loop by first checking that the resource at the desired location exists before rewriting, instead of checking that the current request does not map to a file (or directory). This also involves just one filesystem check rather than two.
For example:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/crud/views/pages/$1 -f
RewriteRule (.*) crud/views/pages/$1 [L]
The first condition that checks against the REDIRECT_STATUS environment variable just makes sure that only direct requests are check and not rewritten requests by the rule itself.

Rewrite URLs using .htaccess to a subfolder

my problem is that I moved my entire website to a subfolder of the public-html one. Tried to rewrite the URLS to automatically add the subfolder via the .htaccess but didn't succeed
this is the code :
ErrorDocument 401 "Unauthorized"
ErrorDocument 403 "Forbidden"
RewriteEngine On
RewriteBase /
DirectoryIndex index.php index.cgi index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ anb/$1 [L,QSA]
Thanks in advance
I got it to work by deleting the line
RewriteBase /
Apparently the RewriteBase doesn't work well when the .htaccess is placed in the root directory A.K.A [public_html]

Apache is redirecting folder to file .html with the same name

I'm trying to redirect 404 json fales to 404.json (with "{}" as content). But apache is redirecting folder to a html file with the same folder name and any custom 404.json redirect fails.
File structure:
/example1/
/example2/
/example1.html
/example2/data.json
/data.json
Working (redirect to 404.html):
http://example.com/example3/
Working (redirect to 404.json):
http://example.com/not-exists.json
http://example.com/example3/data.json
http://example.com/example2/data.json
Working (open the file):
http://example.com/example1.html
http://example.com/data.json
http://example.com/example2/ (list files in folder)
Not working (default apache 404):
http://example.com/example1/
http://example.com/example1/not-exists.json
http://example.com/example2/not-exists.json
It seems apache is redirecting "example1/" to "example1.html", especially when I try to access a file inside the directory like http://example.com/example2/data.json, shows 404 message:
The requested URL /example2.html/data.json was not found on this server.
It happens even without the .htaccess! All the problem because the example1.html file.
htaccess:
# Rewrite MOD
Options +FollowSymLinks
RewriteEngine On
# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
# 404 json
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)\.json$ 404.json [L]
# 404 page
RewriteRule . 404.html [L]
Maybe it is some apache file mapping configuration but I can't find anything about it. It happens only for deploy server, for localhost it's fine. =/
Have it like this with MultiViews turned off:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# 404 json
RewriteRule \.json$ 404.json [L,NC]
# 404 page
RewriteRule ^ 404.html [L]

.htaccess in subdirectory always refers to root

I'm facing a quite strange behaviour of my .htaccess. Whenever I try to access a link rewritten by mod_rewrite, my .htaccess is refering to the root and not the subdirectory I'm working in. My folder structure looks like that:
htdocs/
blog6/
.htaccess
My .htaccess looks like that:
Options +FollowSymLinks -MultiViews
ErrorDocument 401 /core/error/401.php
ErrorDocument 403 /core/error/403.php
ErrorDocument 404 /core/error/404.php
ErrorDocument 500 /core/error/500.html
RewriteEngine on
RewriteBase /blog6/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
But whenever I try to access a file through a rewritten url, I get a 404 error and the log says:
C:/xampp/htdocs/index.php' not found or unable to stat
So it seems like my .htaccess wants to access the file in the htdocs instead of using the subdirectory. When I write /blog6 in my rewriteRule, everything works fine, but why RewriteBase isn't working properly? If it's important, I'm using <base> in my html
RewriteBase works only if you provide relative URL in target of RewriteRule so change your code to:
Options +FollowSymLinks -MultiViews
ErrorDocument 401 /core/error/401.php
ErrorDocument 403 /core/error/403.php
ErrorDocument 404 /core/error/404.php
ErrorDocument 500 /core/error/500.html
RewriteEngine on
RewriteBase /blog6/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA]

.htaccess and redirecting nonexistent files and directories

I am trying to figure out how to program a .htaccess file to redirect any file or directory that does not exist to the index.html file in the web root.
I sort of figured it out with this directive:
ErrorDocument 404 /index.html
The problem is that an HTTP 404 error is still returned by the web server. I'd like to show a permanent redirect for all these files instead. It is important that a 404 error NOT be returned.
Suggestions appreciated!
Try adding this rule to your htaccess file instead:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ /index.html [L,R=301]
This checks that the request isn't a file, isn't a directory, and isn't a symlink. Then redirects whatever the request is to /index.html.
There is a convinient FallbackResource directive in Apache
FallbackResource /index.html