Custom 404 page using .htaccess - .htaccess

I want custom 404 page for my website. I already tried this code.
ErrorDocument 404 /404.php
It's not working and showing "File not found error"
Custom 404 page is: https://www.hostitbro.in/404/
I am using this code in htaccess to remove .php from URLs
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)/$ $1.php [NC,L]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
I think this code conflict with 404 error htaccess code. anyone can fix this for me?

You can use RedirectMatch:
RedirectMatch 404 ^/abc/.*$

Related

.htaccess rewrite index GET request

So, I want to rewrite my url from this:
http://example.com/l1
to
http://example.com/?loc=l1
Currently I have this:
RewriteEngine On
RewriteRule ^/(.*)$ ./?loc=$1 [NC,L]
ErrorDocument 404 /errors/404.php
But each time I type http://example.com/l1 it gives me a 404 error! Please help!
Use this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /?loc=$1 [NC,L]
ErrorDocument 404 /errors/404.php
I do not rewrite the links to existing files with !-f.
And URI in RewriteRule does not begin with a /

Show 404 page with redirecting to www only

I want to change the 404 error. I redirected the 404 error page to a custom one, without redirecting the URL itself. This works, see (http://www.gincher.net/not.exist).
I'm using Google PageSpeed, which works only when the user enters from www. So I add a redirect to add a www, which works for me.
The problem starts when I combine them (http://gincher.net/not.exist). It gives this error:
Moved Permanently
The document has moved here (gincher.net/not.exist).
Additionally, a 500 Internal Server Error error was encountered while
trying to use an ErrorDocument to handle the request.
Here is my HTACCESS code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^gincher.net$
RewriteRule (.*) http://www.gincher.net$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /error.php [L]
Please advice!
First of all, you're redefining the behaviour of ErrorDocument.
Your rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /error.php [L]
is the same as
ErrorDocument 404 /error.php
Then, you still have a syntax error in your first rule.
Replace your current code by this one
ErrorDocument 404 /error.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^gincher\.net$ [NC]
RewriteRule ^(.*)$ http://www.gincher.net/$1 [R=301,L]
Note: you also have the possibility to write the same rule without hard-coding your domain name
ErrorDocument 404 /error.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

How to redirect an error 404 to home page with .htaccess?

Hello how do I redirect an error 404 to a home page with .htaccess?
Example:
site.com if write site.com/some_site_notforund instead of 404 redirects us to the main page
Example 2:
sadistic.pl if write sadistic.pl/some_site_notfound instead of 404 redirects us to current page
Try:
FallbackResource /index.html
or whatever the homepage is
try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]
You can do this like
this will be .htaccess file:
ErrorDocument 404 /404.php
All the page not found pages will display 404.php file.
In 404.php file you can write:
<?php
header('location:index.php');
?>
So all page not found pages will go to 404.php page and it will redirect them to index.php
Or save a reroute step and just put the following into your .htaccess file:
ErrorDocument 404 /index.php
Make a .htaccess file in your root directory.
Put this code into it and customize the RewriteRule.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ http://127.0.0.1/dir/index.php [L]
Works perfectly on my localhost.
It works fine for my site. I tried this code, it doesn't show 404 pages, but it shows the same URL but content of the homepage
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]

Rewrite to custom 404 page if .php is entered in the address bar

I have a htaccess file in my web server. It hides .php on all my files but if I go to page1.php it shows page1.php. I want it to rewrite to 404 page instead
Here's my htaccess file so far. it reoves .php and 404.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
ErrorDocument 404 /spel/template/404

.htaccess redirect of index.php is breaking 404 pages

I have recently setup a index.php redirect through .htaccess. The idea here is to negate duplicate content issue that crops up when a site has both an index.php and / (homapage) getting indexed.
Here is my original .htaccess
Options -Indexes
ErrorDocument 403 /customerrors/403.html
ErrorDocument 401 /customerrors/401.html
ErrorDocument 400 /customerrors/400.html
ErrorDocument 500 /customerrors/500.html
ErrorDocument 404 /customerrors/404.html
Pretty basic.
I used the technique listed here to redirect the index.php to /.
http://www.askapache.com/htaccess/redirect-index-blog-root.html
It works great too. One issue is, it breaks the 404 pages.
This is the modified .htaccess that is breaking the 404 pages.
RewriteEngine On
RewriteBase /
DirectoryIndex index.php
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Options -Indexes
ErrorDocument 403 /customerrors/403.html
ErrorDocument 401 /customerrors/401.html
ErrorDocument 400 /customerrors/400.html
ErrorDocument 500 /customerrors/500.html
ErrorDocument 404 /customerrors/404.html
So if a user types in or goes to www.example.com/dafjkadbfda instead of serving 404 page, the URL remains the same(in this case the broken one) and severs the index.php page.
This in turn is opening another can of worms. All those broken pages are coming up as duplicate content and meta.
Is there another way to write the .htacess redirect that will take into account the 404 pages? Seems like that is the conflict here.
Thanks in advance.
This part of the .htaccess is the one that "breaks" the 404:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You could manage the problem by sending a 404 error from your PHP script:
if ($not_a_valid_page){
header("$_SERVER[SERVER_PROTOCOL] 404 Not Found");
readfile("./customerrors/404.html");
die();
}

Resources