繁体   English   中英

使用 htaccess 将 url 以.php 页面重定向到自定义 404 页面导致自定义 404 页面损坏

[英]redirect url ends with .php page to custom 404 page using htaccess caused the custom 404 page broke

RewriteEngine On
ErrorDocument 404 /exception/404.php
//if url ends with .php/ , show custom 404 page
RewriteRule .php/$ - [R=404,L]
//this line caused the custom 404 page broken
RewriteRule .php$ - [R=404,L]

在我添加第 4 行之前:speedcubing.top/index.php

(显示索引页)

speedcubing.top/index.php/

(显示自定义 404 页面)

在我添加第 4 行之后: speedcubing.top/index.php speedcubing.top/index.php/

他们都显示:

未找到

在此服务器上未找到请求的 URL。

此外,在尝试使用 ErrorDocument 处理请求时遇到 404 Not Found 错误。

ErrorDocument指令和RewriteRule指令来自在不同时间运行的不同 Apache 模块。 因此设置R=404不会导致 Apache 调用ErrorDocument处理程序,它最终显示默认的404 Apache 处理程序。

您应该在/exception/404.php中添加此行以设置自定义 http 响应代码:

<?php
http_response_code(404);
// rest of the code
?>

并让您的 .htaccess 代码如下:

ErrorDocument 404 /exception/404.php

RewriteEngine On

# if url ends with .php or .php/, show custom 404 page
RewriteCond %{REQUEST_URI} !^/exception/404\.php$ [NC]
RewriteRule \.php/?$ exception/404.php [NC,L]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM