htaccess file does not take me to 404 error page - .htaccess

I have created a .htaccess file and placed it in my root directory for 404 error page, but when I type a random subdirectory after my URL, it takes me to the index.php file instead of my 404.php file. I have the following code in my .htaccess file.
ErrorDocument 404 /error/404.php
What am I doing wrong?

/error/404.php indicates your file location is /var/www/html/error/404.php
If the name of your project directory is, lets say myweb, then your .htaccess should be
ErrorDocument 404 /myweb/error/404.php

Related

htaccess Redirect when 404

I have a subdirectory called /marker, in it are some png files. Now I want the file /marker/standard.png to be returned whenever an attempt is made to call a png that does not exist. In other words: If, for example, /marker/doesntexist.png is called (which doesn't exist), /marker/standard.png should be delivered.
Sounds quite simple, but several attempts with RewriteRule or ErrorDocument failed.
Any ideas?
tia
You can do the following to serve /marker/standard.png if another .png file in the /marker directory is requested that does not exist.
This uses mod_rewrite and should go near the top of the .htaccess file in the document root.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^marker/(?!standard\.png$).+\.png$ marker/standard.png [L]
I took the extra step to exclude requests for standard.png itself (using a negative lookahead), in case this doesn't exist for some reason.
Note that this is a case-sensitive string comparison. eg. Only .png files are matched, not .PNG.
This naturally results in a 200 OK response when standard.png is served. If you still need a 404 Not Found status then use ErrorDocument instead (see the next section).
Using ErrorDocument
You also mentioned the use of an ErrorDocument directive. This is another solution, although it will result in a 404 HTTP response status, which may or may not be desirable.
For example, in order to restrict this ErrorDocument just to the /marker subdirectory then create an additional .htaccess file in this subdirectory with the following:
ErrorDocument 404 /marker/standard.png
This will naturally serve standard.png for any non-existent request to the /marker subdirectory. To restrict this to .png requests only then wrap the ErrorDocument in an <If> expression. For example:
<If "%{REQUEST_URI} =~ /\.png$/">
ErrorDocument 404 /marker/standard.png
</If>

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

Redirect correctly all folders to root site when 403 error occurs

I have my site: https://stackoverflow.com/ and a subdomain https://foo.stackoverflow.com/.
My folder public_html/foo has subfolders:
public_html/foo/folder_1
public_html/foo/folder_2
public_html/foo/folder_3
Now, my public_html/.htaccess file
Options -Indexes
ErrorDocument 403 /403.php
RewriteEngine on
RewriteRule ^403.php$ https://foo.stackoverflow.com/ [R=301]
The rule above, works fine when I access https://stackoverflow.com/foo/folder_1 the page is redirected to https://foo.stackoverflow.com/ BUT when I access https://foo.stackoverflow.com/folder_1 the page is not redirected and the default 403 error page is displayed.
What can I change in my public_html/.htaccess file, to solve this problem?
Please, I want a minimalist solution.

Https 404 Redirection in htaccess

Please tell me how to redirect 404 page of following example
https://www.example.co.in/
Create a php page and call name it 404.php and put your redirect urls.
Open .htaccess file and add the following:
ErrorDocument 404 /404.php
All done.

redirect 403 error using .htaccess

I want to redirect any 403 using .htaccess, but it does not seem to work.
Options +FollowSymlinks
RewriteEngine on
ErrorDocument 403 notfound.html
RewriteRule notfound.html
All help appreciated.
Thanks
Jean
The URL part of an ErrorDocument directive should either start with a leading slash, which indicates a path that's relative to your DocumentRoot, or it should be a full URL (if you want to use an external document).
You shouldn't need the RewriteEngine and RewriteRule directives at all here.
So, assuming your notfound.html is at the root level of your site, your directive should look like:
ErrorDocument 403 /notfound.html
I found a problem with the earlier example, which was:
ErrorDocument 403 /notfound.html
I have a directory on my site storing images, say at domain.com/images
I wanted that if someone tries accessing the directory's URL that they be redirected to the site's homepage. My problem was, with the above, that the URL in the browser would remain domain.com/images and then the homepage would load, but the homepage would reference stylesheets that it could not access from that URL. (And it makes sense - 403 is supposed to show an error message).
I then tried researching how to redirect a 403 error to another URL and a few sites said you can't do a 302 or 301 - you're missing the point: 403 is an error, not a redirect.
But I found a way. This is the .htaccess file inside the /images directory:
Options -Indexes
ErrorDocument 403 /images/403.php
RewriteEngine on
RewriteRule ^403.php$ /index.php [R=301]
So basically the first line prevents directory listing.
The second line says that for "permission denied" (ie. 403 error, which is what the first line gives), the site should load the file "/images/403.php". But you don't actually need to create any 403.php file in that directory, because...
The third line turns on your redirect engine
And the fourth line says that if the user attempts to load the page 403.php (ie. in this "/images" directory where this .htaccess file is located) then they should be redirected, using a 301 (permanent) redirect, to /index.php - which is the index of the root directory (ie the homepage), and not the index of the /images subdirectory (and indeed there is no index in "/images" anyway).
Hence - how to get a 403 to redirect to another URL using a 301 (permanent) redirect (or, you could try a 302 temporary redirect too).
Here is a super easy way to do this:
Use this line
Options -Indexes
ErrorDocument 403 /goaway.html
where goaway.html contains the following line in the head section:
<meta HTTP-EQUIV="REFRESH" content="0; url=HERE YOU PUT THE URL YOU ARE REDIRECTING TO" />
And that's it... each 403 error will go to your goaway.html file in root directory and goaway.html will redirect them to the URL of your choice
RewriteEngine On
ErrorDocument 403 http://www.yourdomain.example/

Resources