Useragent redirect if 404 - php

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]

Related

custom 404 page htaccess without redirect

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

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

Add status code 410 for missing images in Drupal 6

I need to send status 410 for missing images instead of 404, in Drupal 6.
For example I have a image link as https://www.example.com/our-locations/directory/sub-directory/files/xyz.png which no longer exist in this location, then I need to send status 410 instead of 404.
Solutions already I have tried are:
RewriteCond %{REQUEST_URI} ^our-locations/directory/sub-directory/files/(\.jpg|\.png)$ [NC]
RewriteRule ^(.*)$ - [NC,R=410,L]
You can use this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/path/.+\.(jpg|png|gif)$
RewriteRule ^ - [R=410,L]
The first condition RewriteCond %{REQUEST_FILENAME} !-f checks if the the file doesn't exist. Since you want to redirect only 404 images to 410 this condition prevents your other existing files from being redirected to 410. The next condition
RewriteCond %{REQUEST_URI} ^/path/.+\.(jpg|png|gif)$ checks if the uri is /path/image.ext if both conditions are met then the RewriteRule is applied.
You can also use <filesMatch> and ErrorDocument directives to catch a 404 image file and then redirect it to a specific page but this will not return the correct error status. You can use RewriteRule instead .
<filesMatch "\.(jpg|png|gif)$">
ErrorDocument 404 http://exmaple.com/410.php
</FilesMatch>

Custom error page failed to load after editing htaccess

I edited my htaccess file to rewrite mysite.com/page to mysite.com/page.php and to show 404 error page if a user requested mysite.com/page.php instead of mysite.com/page
I also added my own custom 404 error page
But when I requested mysite.com/page.php, the error page shown was Apache default 404 error page (instead of my custom error page), with additional error info:
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
My error log:
Request exceeded the limit of 10 internal redirects due to probable configuration error.
Here is my htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
ErrorDocument 404 /error/404
I have tried changing 404 at the last line above to 404.php but the custom error page still not showing up..
However, the custom error page did show up when I requested mysite.com/page/ (notice the slash).
I don't understand, where this error comes from. But you can resolve it by exiting the rule chain when 404.php is requested. Insert this rule at the beginning
RewriteRule ^error/404.php$ - [L]
Unrelated, but you can simplify your rules a little bit
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]
RewriteCond %{THE_REQUEST} \.php
RewriteRule \.php$ - [L,R=404]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php
ErrorDocument 404 /error/404.php

Categories