Custom error page failed to load after editing htaccess - php

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

Related

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>

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]

How to disable default 404/401/400.. error messages in CodeIgniter?

I want to use htaccess to show 404/401/400 ... error messages but my website always shows errors which CI default error messages. I dont want to use CI error function or custom it. Is the anyway to disable that ?
I tried this in my htaccess file but not working.
RewriteEngine on
ErrorDocument 404 /404.html
One solution is to use a rewriteRule for your error pages.
RewriteRule is faster then the ErrorDocument directive.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /404.html [NC,L]
This will rewrite all non existent dirs and files to /404.html

Return 404 page when someone access any .php? [duplicate]

This question already has an answer here:
Redirect to 404 if .php extension is requested in URL
(1 answer)
Closed 7 years ago.
I always hide the files .php extension in .htaccess. However, if anyone access the .php file returns a unexpected error page that should be 404 page in the current .htaccess text.
My actual text (from #anubhava answer):
# to present a custom 404 page
ErrorDocument 404 ./error
RewriteEngine On
# return 404 for direct .php requests
RewriteCond %{THE_REQUEST} \.php[/\s?] [NC]
RewriteRule ^ - [R=404,L]
# silent rewrite to add .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Just a test. Edit: I believe the error is on the 404 document, as the 404 error is being returned now. Instead of appear the 404 page, it appears the directory I input in my browser page, as below:
./error
Here is a rule you place just below RewriteEngine On to present 404 for the requests for .php files:
# to present a custom 404 page
ErrorDocument 404 /error
RewriteEngine On
# return 404 for direct .php requests
RewriteCond %{THE_REQUEST} \.php[/\s?] [NC]
RewriteRule !^error - [R=404,L]
# silent rewrite to add .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Categories