在PHP中显示自定义404错误页面而不重定向

Ezh*_*hil 3 php .htaccess

我创建了名为error.php的自定义404错误页面,现在我想在用户输入的url.like这个链接中显示error.php内容:http://www.youtube.com/asdfasfsd

这是我的htaccess代码:

ErrorDocument 404 https ://localhost/path/error.php
Run Code Online (Sandbox Code Playgroud)

我想在同一个URL中显示error.php内容而不重定向到error.php页面

如果用户键入了无效的URL(例如:https://localhost/path/nnn.php)

当前结果:重定向到error.php

预期结果:在https://localhost/path/nnn.php中显示error.php内容

我的完整htaccess代码:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteRule index.html$ index.php [L]
RewriteRule service.html$ service.php [L]
RewriteRule ^([^/]*)\.html$ about.php?pid=$1 [L]
RewriteRule ^cont/([^/]*)\.html$ contact-inner.php?tid=$1 [L]
RewriteRule ^contact/([^/]*)/([^/]*)\.html$ contact-page.php?tid=$1&ona=$2 [L]
RewriteRule ^about/([^/]*)\.html$ about-inner.php?oid=$1 [L]
RewriteRule ^service/([^/]*)/([^/]*)\.html$ service-page.php?oid=$1&ona=$2 [L]



ErrorDocument 404 https://localhost/ezhil/path/public_html/error.php

<Files 403.shtml>
order allow,deny
allow from all
</Files>

deny from 117.202.102.84
deny from 58.68.25.210

deny from 91.200.13.112
deny from 86.128.130.170
deny from 91.200.13.7
deny from 173.208.206.90
Run Code Online (Sandbox Code Playgroud)

Dan*_*all 6

请删除您的ErrorDocument规则并将其替换为以下代码:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ error.php [L]
Run Code Online (Sandbox Code Playgroud)

顺便提一句:你应该[L]抛弃RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI},这样任何其他规则都不会覆盖HTTPS强制执行.

你的.htaccess意志看起来像这样:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

RewriteRule index.html$ index.php [L]
RewriteRule service.html$ service.php [L]
RewriteRule ^([^/]*)\.html$ about.php?pid=$1 [L]
RewriteRule ^cont/([^/]*)\.html$ contact-inner.php?tid=$1 [L]
RewriteRule ^contact/([^/]*)/([^/]*)\.html$ contact-page.php?tid=$1&ona=$2 [L]
RewriteRule ^about/([^/]*)\.html$ about-inner.php?oid=$1 [L]
RewriteRule ^service/([^/]*)/([^/]*)\.html$ service-page.php?oid=$1&ona=$2 [L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ error.php [L]


<Files 403.shtml>
order allow,deny
allow from all
</Files>

deny from 117.202.102.84
deny from 58.68.25.210

deny from 91.200.13.112
deny from 86.128.130.170
deny from 91.200.13.7
deny from 173.208.206.90
Run Code Online (Sandbox Code Playgroud)