.htaccess redirect error page problems - .htaccess

I am using the following code in my .htaccess:
ErrorDocument 404 error_page.php?msg=404
When I trigger a 404 error, I get a page that says this:
error_page.php?msg=404
Is there something that I am doing wrong?
Thanks

Not sure if you can pass query strings to the document designated by ErrorDocument, Something you can try is using it in conjunction with mod_rewrite:
ErrorDocument 404 /error-doc-404
ErrorDocument 405 /error-doc-405
ErrorDocument 500 /error-doc-500
RewriteEngine On
RewriteRule ^error-doc-([0-9]{3})$ /error_page.php?msg=$1 [L]
Since this is in a subdirectory off of the document root, it changes everything.
Say the subdirectory is /something/
ErrorDocument 404 /something/error-doc-404
ErrorDocument 405 /something/error-doc-405
ErrorDocument 500 /something/error-doc-500
RewriteEngine On
RewriteRule ^error-doc-([0-9]{3})$ /something/error_page.php?msg=$1 [L]
Assuming that your error_page.php file is actually in the /something/ subdirectory.

Related

redirect url ends with .php page to custom 404 page using htaccess caused the custom 404 page broke

RewriteEngine On
ErrorDocument 404 /exception/404.php
//if url ends with .php/ , show custom 404 page
RewriteRule .php/$ - [R=404,L]
//this line caused the custom 404 page broken
RewriteRule .php$ - [R=404,L]
before I added line 4:
speedcubing.top/index.php
(show the index page)
speedcubing.top/index.php/
(show custom 404 page)
after I added line 4:
speedcubing.top/index.php
speedcubing.top/index.php/
they both showing:
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
ErrorDocument directive and RewriteRule directive are from different Apache modules that run at different times. Hence setting R=404 doesn't cause Apache to invoke ErrorDocument handler and it ends up showing default 404 Apache handler.
You should add this line in your /exception/404.php to set a custom http response code:
<?php
http_response_code(404);
// rest of the code
?>
And have your .htaccess code like this:
ErrorDocument 404 /exception/404.php
RewriteEngine On
# if url ends with .php or .php/, show custom 404 page
RewriteCond %{REQUEST_URI} !^/exception/404\.php$ [NC]
RewriteRule \.php/?$ exception/404.php [NC,L]

throw 404 error with .htaccess file and php

I've got url like www.web.com/home and www.web.com/about with .htaccess file:
RewriteEngine On
Options +FollowSymLinks
ErrorDocument 404 http://www.web.com/page-not-found.html
RewriteRule "home" "index.php?c=home&m=main"
RewriteRule "about" "index.php?c=home&m=about"
If I type something like www.web.com/asd, .htaccess will throw 404 error and direct page-not-found.html file
But, If I type www.web.com/homesss or www.web.com/about/a/b/c, the .htaccess will not throw the 404 error
How do I fix that? I use pure PHP
Use meta characters in rewrite rule to 1:1 match:
^ --- Matches the beginning of the input
$ --- Matches the end of the input
[L] --- Flag last to stop processing rule after match
RewriteEngine On
Options +FollowSymLinks
ErrorDocument 404 http://www.web.com/page-not-found.html
RewriteRule ^home$ index.php?c=home&m=main [L]
RewriteRule ^about$ index.php?c=home&m=about [L]
In addition to #JarekBaran's answer...
If I type something like www.web.com/asd, .htaccess will throw 404 error
Not with the code you've posted...
ErrorDocument 404 http://www.web.com/page-not-found.html
When a request does not map to a resource, the above triggers a 302 (temporary) redirect to the http://www.web.com/page-not-found.html. There is no 404 HTTP response. Details of the request that triggered response are lost. /page-not-found.html is exposed to the end user. This is generally bad for SEO and delivers a bad user experience.
The 2nd argument to the ErrorDocument directive should be a root-relative URL-path, starting with a slash so that the error document is called with an internal subrequest. Apache then responds with a 404 status. The error document is not exposed to the end user.
(Very rarely should an absolute URL be used here.)
For example, it should be like this instead:
ErrorDocument 404 /page-not-found.html
Reference:
https://httpd.apache.org/docs/2.4/mod/core.html#errordocument

URL changes when using ErrorDocument 403 in .htaccess

I have written a .htaccess file to make sure that visitors are always redirected to https and to www. In addition to that I have also added a custom html page for 404 errors.
When visitors try to access a forbidden file I want them to see my custom 404 message, as to not reveal that the path contains a forbidden file.
Here is the problem. When writing for example "example-domain.com/.htaccess" (no www or https) in the browser, the URL in the address field in the browser changes to "https://www.example-domain.com/missing.html". But I want it to say "https://www.example-domain.com/.htaccess" while displaying my 404 page.
It works for 404 errors. But when typing in a path in the address field which both triggers the 403 error and fulfill at least one of the rewrite conditions in my .htaccess file (missing https and/or www) I experience the above described problem.
Here is the code in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example-domain.com [NC]
RewriteRule ^(.*)$ https://www.example-domain.com/$1 [L,R=301,NC]
ErrorDocument 403 /missing.html
ErrorDocument 404 /missing.html
Best regards

how to get current path of .htaccess file

I want to set the path of 404 error file inside .htaccess file without typing the domain normaly inside it every time when I change the domain.
For example :
This is normal code ErrorDocument 404 http://www.example.com/404.php
I want to set it to ErrorDocument 404 404.php
I mean widthout domain url .
Is there an variable in .htaccess like $_SERVER['HTTP_HOST'] in php to get domain url ?
Whene I set the 404 code as ErrorDocument 404 404.php its show empty page and inside it text like "404.php" only .
thank you for help ^_^ .
This is what I use in my .htaccess file. Hope it helps.
# This redirects browsers that experience a 404 error (Not Found) (and others listed)
# to the file "error.php" located in the same directory as your .htaccess file.
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

htaccess error 404 handling not working + dynamic 404 error page?

First of all, my ErrorDocument 404 /v1 is never redirecting, but if I look in the Chrome Inspector, I really have a 404 error. At first I thought it was because I really needed to specify a file ex: error.html, but that didn't work either. /v1 is just a wordpress subdomain, and I want to redirect all of my errors there. /v1 falls on my homepage of my subdomain.
Also I would like to make the error page dynamic, but I can't figure how to do that. Something like that:
RewriteCond %{REQUEST_FILENAME} public_html/(.*)
ErrorDocument 404 /v1/%1
Any ideas? Thank you!!
You're goinmg to need to return the 404 through a php script:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /404.php?path=$1 [L]
And in the 404.php file:
<?php
header("HTTP/1.0 404 Not Found");
print('The file that you requested: ' . $_GET['path'] . ' was not found\n');
// or include a special 404 page
include ('/path/to/' . $_GET['path']);
?>
Or you could lose the ?path=$1 part completely and just look in $_SERVER['REQUEST_URI'].
This syntax is incorrect:
RewriteCond %{REQUEST_FILENAME} public_html/(.*)
ErrorDocument 404 /v1/%1
Because ErrorDocument has no connection with RewriteCond (mod_rewrite) and it is unconditional.
Conditional 404 handling example:
ErrorDocument 404 /404.php
RewriteRule ^foo/ - [L,NC,R=404]
I use this syntax! Usually works!
ErrorDocument 404 "<script>window.location.href='404.php?page='+window.location.href.split('/')[window.location.href.split('/').length-1];</script>"

Resources