custom 404 page htaccess without redirect - php

I have a particular routing system:
index.php
<?php
// template start include
if(!empty($_GET['page'])){
$path = "pages/".$_GET['page']."/index.php";
if(file_exists($path)){
include $path;
}else{
ob_end_clean();
header('HTTP/1.0 404 Not Found');
exit();
}
}
// template end include
?>
.htaccess
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ index.php?page=$1 [L,QSA,B]
That means:
if a user go to https://example.com/account/
the content is taken from /myserver/mysite/pages/account/index.php
or
If a user go to https://example.com/pagethatnotexists/
a "standard" 404 error will be shown.
I made a custom 404 php page on: /myserver/mysite/404.php
and what I need is to show my custom 404 page without redirect the user. The URL should be always https://example.com/pagethatnotexists/
I know what you think, I could include this page on the else clause in my index page, but the 'template start' and 'template end' will be shown, and I don't want this.
I tried adding this line on the .htaccess:
ErrorDocument 404 /myserver/mysite/404.php
but the user will be redirect to this url
I tried with this line in before and after my rewriteRule
RewriteRule ^(.*)/$ https://%{SERVER_NAME}/mysite/404.php
But I always see the 404 page even if the page exist.
How can I do this?
Thanks in advance

Related

How to redirect any links valid or invalid in my website to the index.php?

I have a website its domain name is : http://cabinets.ga
My website is a single page application using ajax to do all of its requests.
I want for example if the user writes on the browser http://cabinets.ga/login/login.php or any other link (directory or file, exists or not exists) the .htaccess redirect it to the index.php because i want that all of my requests in my website will only be with jquery ajax.
This is my Code but it doesn't work exactly:
Options
+SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
*Any update on my code or any other code or way to do that please?
ErrorDocument 404 /404.php
Create page 404.php
in that page just redirect to your index.php
using php
header("Location: index.php")
To redirect all requests to /index.php you can use the following :
RewriteEngine on
RewriteRule !index\.php /index.php [L,R]

How to return a 404 for nonexistent WordPress http pages rather than 301 and then 404

Someone has created thousands of backlinks to non-existing pages of my website. Unfortunately, Googlebot tried to crawl these pages.
My website is in HTTPs. Googlebot hits the HTTP version of the page, my server first returns a 301 redirect and then, it returns a 404 at the second Googlebot query. It slows down crawling a lot. In fact, right now, Googlebot sees the 301 and decides not to follow the redirection right away.
I wonder if it would be possible to return a 404 before returning a 301 maybe by adding a directive in the Htaccess file. Meanwhile, I would like the server to return a 301 redirect to HTTPS when Google tried to visit the HTTP version of an existent WordPress page.
Example:
So, H T T P ://example.com/jdfjdfd/ does not exist. Googlebot visits it and returns a 301, and then my server returns a 404 when Googlebot visits H T T P S://example.com/jdfjdfd/
I would like that my server returned a 404 error page when Googlebot visits H T T P://example.com/jdfjdfd/
I tried to add the redirection directive after the WordPress directive but it does not work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTPS} off
rewriterule ^(.*)$ https://example.com/$1 [R=301,L]
but it does not work.
maybe it can only be done in PHP with something similar to this with a modification in functions.php?
// add_action('template_redirect', 'redirect_core', 5000);
// add_action('init', 'redirect_core', 5000);
// add_action('wp_loaded', 'redirect_core', 5000);
function redirect_core(){
if (!is_ssl() ) {
wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301);
exit();
}
}
Based on your shown samples, could you please try following.
Please clear browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTPS} off
Rewriterule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Keeping rule for applying https at top is always recommended.

How Redirect 404page to homepage in https without changing url

I want redirect don't exist page to homepage without changing URL.
I use the following code in .htaccess file.
ErrorDocument 404 /index.php
.htaccess works well for http (it doesn't change the URL), but with https changes the current url.
If you want to include index.php in case of errors without changing URL you can use the following code in .htaccess:
ErrorDocument 404 /index.php
This code is valid for http and https.
If you want to redirect the http requests to https you can add the following code in your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]
Redirect an error message:
Instead of prompting a 404 Not Found error page, the site will redirect to the homepage:
Redirect 404 Error pages to the home page
ErrorDocument 404 http://example.com/
Redirect a non-existing page to index.php
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You can manually insert the html code below to the 404.php page as well.
<head>
<meta http-equiv = "refresh" content = "2; url = https://www.URL.com/404.php" />
</head>

htaccess 404 and 400 Redirect Not Working for All Page on Website

I am setting 404 (400.php) and 400 (400.php) pages.
Website is https://www.rsseosolution.com
If i am typing https://www.rsseosolution.com/sdfsdfsdfsadfsda its working fine and showing 404.php page
But if i am trying to check any not existing php file like https://www.rsseosolution.com/monthly-link-building-campaign.php then its showing normal "File not found." message not redirecting to 404.php.
And for 400 error its showing normal server "Bad Request Error" While i want to show my custom 400.php.
Htaccess is as below
Options +FollowSymLinks
RewriteEngine on
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule seo-package-(.*)-n-(.*)\.php$ seo-package-detail.php?id=$1&name=$2 [L]
RewriteRule case-studies/(.*)-(.*)\.php$ case-studies.php?project=$1&id=$2 [L]
RewriteRule tutorial/(.*)-(.*)\.php$ tutorial.php?topic=$1&id=$2 [L]
RewriteRule blog-page-(.*)\.php$ blog.php?page=$1 [L]
RewriteRule seo-tutorial-(.*)\.php$ seo-tutorial.php?page=$1 [L]
RewriteRule frequently-ask-question-faq-(.*)\.php$ frequently-ask-question-faq.php?page=$1 [L]
ErrorDocument 404 /404.php
ErrorDocument 400 /400.php
One more thing if i am adding [L] with 404.php and 400.php then server is giving "Internal Server Error" for complete site.
I want to redirect all URL (Page, blog and any other mistyping url) go to 404.php.
For bad request Error like https://www.rsseosolution.com/search/save%2050%%20seo%20budget should go to 400.php ....... Both pages are already on site and working fine if type manually page name.
Please guide me.
Try ErrorDocument 404 https://www.rsseosolution.com/404.php
Using the full link (absolute URLs)
and write this rule in top of htaccess where you start writing rules
This link also have good explanation

Useragent redirect if 404

At the moment if a 404 page is requested by a useragent it returns "Unable to get URl"
What I'm looking for is a script (probably best done in php or .htaccess) that will redirect to a certain page if called from a certain useragent if it's a 404 page.
I think it could also be done with a status type thing as if it's a 404 page it returns Status 404 in the visitor log.
At the moment I use this to simply redirect if it's the useragent.
RewriteCond %{HTTP_USER_AGENT} WinInet
RewriteRule ^index.html$ /pagehere.html [NC,L]
In your .htaccess add this code
ErrorDocument 404 /var/www/website/redirect_404.php
#(Your 404 error redirect file location)
#ErrorDocument 404 http://example.com/redirect_404.html (To redirect to a url)
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/redirect_404/$
RewriteRule ^(.*)$ /var/www/website/redirect_404.php [L]
#The ErrorDocument redirects all 404s to a specific URL
Inn your redirect_404.php file enter this code
<?php
header('Location: /errorfolder/404/'); //Path to your error file
exit;
?>
You can use this rule for non-files and non-directories (similar to 404):
RewriteCond %{HTTP_USER_AGENT} WinInet
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . /pagehere.html [L]

Categories