throw 404 error with .htaccess file and php - .htaccess

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

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]

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

htaccess - redirect 404 error

I would like to redirect in htaccess
https://www.example.com/albumgallery.php?id=8
to https://www.example.com/example-gallery
i've tried :
RewriteCond %{QUERY_STRING} ^id=8$
RewriteRule ^albumgallery.php$ https://www.example.com/example-gallery? [L,R=301]
-- it works , but on page it says
Not Found The requested URL /example-gallery was not found on this
server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
any ideas what is wrong with that ?
You are getting a 404 not found error because www.example.com/example-gallery does not exist on your server. You need to rewrite this url to your existing url. for example when you type
https://www.example.com/albumgallery.php?id=8 into your browser your rule redirects it to https://www.example.com/example-gallery since this new url doesn't exist so your server returns a 404 error status. You need to add an internal RewriteRule to your htaccess to handle your new url.
Put the following right bellow your existing Rule
RewriteRule ^example-gallery/?$ /albumgallery.php?id=8&loop=no [L]
I added an additional perameter loop=no to the Rule`s destination it's to prevent infinite loop error so that the rule can not conflict with your existing RewriteRule.

.htaccess 403 404 redirect returned header SEO

A quick question. I want to use a php file that will deliver pages based on the url. I am using htacess to deal with the error docs but my question is to do with SEO. If I redirect a 404/403 request to pageDispacher.php that then delivers the correct page will the header be a 404/403 at any point? As this is not cool. Do I need to use a rewrite rule instead?
In which case is there is a genuine 404 page how would i return a 404 header?
www.example.com > www.example.com/en/home
www.example.com/en/ > www.example.com/en/home
ErrorDocument 404 /pageDispacher.php
ErrorDocument 403 /pageDispacher.php
This is usable:
RewriteRule ^error error.php [L,QSA]
RewriteRule ^(.*)\.php$ error[L,QSA]

.htaccess redirect error page problems

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.

Resources