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

时间:2014-06-06 11:36:59

标签: php .htaccess

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

这是我的 htaccess 代码:

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

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

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

  

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

     

预期结果:以https显示error.php内容   ://localhost/path/nnn.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

4 个答案:

答案 0 :(得分:6)

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

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

顺便提一句建议:您应该在[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

答案 1 :(得分:1)

修改 .htaccess 文件中的行。只需使用路径和文件名(无主机)

ErrorDocument 404 /path/error.php

它对我有用。

答案 2 :(得分:0)

您可能需要稍微调整现有的htaccess才能使其正常工作

RewriteEngine On

# your existing rules. please note the [L] after each rule 
RewriteRule ^main$ index.php [L]
RewriteRule ^about-us$ aboutus.php [L]
RewriteRule ^whatever$ whatever.php [L]

# anything else that ends with .php
RewriteRule .*\.php$ error.php

一旦满足RewriteRule[L]就会停止进一步执行。然后,如果请求以.php结尾,则error.php将被执行。

答案 3 :(得分:0)

只需在您的.htaccess中添加这样的代码

    ErrorDocument 404 "<script>document.write(atob('[Your Html code convert to base64string]'))</script>"

例如

    ErrorDocument 404 "<script>document.write(atob('PCFET0NUWVBFIGh0bWw+CjxoZWFkPgoJPG1ldGEgY2hhcnNldD0idXRmLTgiPgoJPG1ldGEgaHR0cC1lcXVpdj0iWC1VQS1Db21wYXRpYmxlIiBjb250ZW50PSJJRT1lZGdlIj4KCTxtZXRhIG5hbWU9InZpZXdwb3J0IiBjb250ZW50PSJ3aWR0aD1kZXZpY2Utd2lkdGgsIGluaXRpYWwtc2NhbGU9MSI+Cgk8IS0tIFRoZSBhYm92ZSAzIG1ldG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