首页 > 解决方案 > htaccess not redirecting all not found page or URL on site to 404 pages

问题描述

Try to set 404 page not found error page by htaccess.

Issue is if we are searching for https://www.rsseosolution.com/suraj.html ... Its redirect perfect to 404.php. But if we are searching for https://www.rsseosolution.com/suraj.php ... Its not redirecting to 404.php and giving showing simple text message "File not found.".

In short problem is if extension is not php then its redirecting fine but if .php then its showing File not found.

ErrorDocument 404 https://www.rsseosolution.com/404.php
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 [NC,L]
RewriteRule case-studies/(.*)-(.*)\.php$ case-studies.php?project=$1&id=$2 [NC,L]
RewriteRule tutorial/(.*)-(.*)\.php$ tutorial.php?topic=$1&id=$2 [NC,L]
RewriteRule blog-page-(.*)\.php$ blog.php?page=$1 [NC,L]
RewriteRule seo-tutorial-(.*)\.php$ seo-tutorial.php?page=$1 [NC,L]
RewriteRule frequently-ask-question-faq-(.*)\.php$ frequently-ask-question-faq.php?page=$1 [NC,L]

What i am looking for is ....... redirect all not found or wrong URL to 404.php.

What i am missing here.

I think because we are rewriting some URLs with .php extension from htaccess and thats why for PHP its saying file not found but for other its redirecting perfectly to 404.php.

Is there any ways to set 404.php for all not found pages and URL without change any extension (.php) of previous written file by htaccess.

标签: .htaccess

解决方案


问题与您当前在.htaccess.

问题很可能是由于您的服务器配置和在您的服务器上实现 PHP 的方式。如果文件请求.php被代理到后端服务器进行处理,就像 FastCGI 类型配置的情况一样,那么这个 404 Not Found 消息很可能来自后端服务器,而不是您的 Apache 服务器。

通常,您可以使用ProxyErrorOverride反向代理配置中的指令解决此问题:

ProxyErrorOverride On

如果您无权访问主服务器配置,那么您可以在.htaccess请求发送到代理服务器之前使用 mod_rewrite in 提前触发 404。

例如,在您现有的 mod_rewrite 指令之前:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.php$ - [R=404]

但是,这严格来说是一种“解决方法”,如果您有权访问服务器配置,那么ProxyErrorOverride最好使用(如上所述)。


在旁边:

ErrorDocument 404 https://www.rsseosolution.com/404.php

通过在指令中指定绝对 URL,ErrorDocument这会触发错误文档的外部(302) 重定向(附加请求),这通常是不可取的(并且您需要手动设置 404 响应代码)。最好将错误文档作为内部子请求发出:

 ErrorDocument 404 /404.php

推荐阅读