4

I created custom 404 error page called error.php, now I want to display the error.php content in user entered url.like this link: http://www.youtube.com/asdfasfsd

This is my htaccess code:

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

I want to show the error.php content in same URL without redirect to error.php page

if user typed invalid url (for example:https ://localhost/path/nnn.php)

current result:redirecting to error.php

expected result:display error.php content in https ://localhost/path/nnn.php

My full htaccess code:

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
Ezhil
  • 996
  • 8
  • 13
  • you can include error.php in same page... – Mani Jun 06 '14 at 11:40
  • this must be done using `.htaccess`. if no `RewriteRule` is met, then execute `error.php`. The `url` will stay the same, but the `error.php` will be executed. no time for me to answer now, lunch break :P – Sharky Jun 06 '14 at 11:49
  • This question appears to be off-topic because it is about configuring apache, not about programming (in php) – PlasmaHH Jun 06 '14 at 12:39
  • Yes, it is not specific to PHP. But "configuring Apache" is something different to me. Configuring Apache means to me editing `/etc/apache2/` (as server administrator), but changing a `.htaccess` is an edit of the web application. The `.htaccess` file is a part of a website resp. a web-application (like a CMS), and therefore it does count to web-development in my opinion. – Daniel Marschall Jun 06 '14 at 12:45

4 Answers4

7

Please remove your ErrorDocument rule and replace it with following code :

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

A suggestion by the way: You should put a [L] behind RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} , so that no other rule will override the HTTPS-enforcement.

Your .htaccess will then look like this:

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
Daniel Marschall
  • 3,739
  • 2
  • 28
  • 67
  • 1
    jesus christ or whatever, dont just copy from other questions http://stackoverflow.com/a/2777272/953684. there are some paths in there (!^subs) from the other question. please pay attention when you answer. you could just link to that answer through a comment. – Sharky Jun 06 '14 at 11:54
  • I had the idea with `mod_rewrite` before you commented above. Don't be that angry just because I have overseen that detail. – Daniel Marschall Jun 06 '14 at 11:56
  • there is no point which had "first" the idea. the point is you copy and paste something from ANOTHER question, that means its GARBAGE for THIS question. if you can't understand why this is bad, i can't help you more. and for the "overseen the detail": the DETAIL makes the difference. if you dont have time for a detailed answer, just post a link to the other answer for further investigation, or get ready for downvotes for "no research effort". – Sharky Jun 06 '14 at 11:58
  • Stop being that aggresive to new users. I am willing to edit my answers. And leave jesus out of your comments, this offends me. – Daniel Marschall Jun 06 '14 at 12:00
  • i have no idea any help? – Ezhil Jun 06 '14 at 12:05
  • What happens if you put the above code in your `.htaccess` file? If you get a HTTP 500 error message, you have probably not installed `mod_rewrite`. – Daniel Marschall Jun 06 '14 at 12:06
  • I have edited the rules above. Does it work now? If not, you might try to modify `/error.php` to `error.php` or `/path/error.php` or change the `RewriteBase`. (This might often an issue with aleady redirected profiles like http://example.com/~user/noexisting.php ) – Daniel Marschall Jun 06 '14 at 12:19
  • its not working again i got object not found error 404 – Ezhil Jun 06 '14 at 12:25
  • Have you replaced your `ErrorDocument 404` rule with the 2 `RewriteCond` and `RewriteRule`? The order is importan. Also, I have now removed the slash / in front of error.php since you didn't use it in your full .htaccess which you have posted. – Daniel Marschall Jun 06 '14 at 12:31
  • I'm glad I could help. Please mask this answer as solution (green check). You might additionally want to change the 4th line to `RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]`, otherwise another rule might override your HTTPS enforcement. – Daniel Marschall Jun 06 '14 at 12:40
  • I use a simplified form of this solution in my .htaccess. There's one important caveat to note. If your web application employs session variables, be sure you have a `favicon.ico` in your web root. Most browsers are OK with not having one, but Google Chrome will forget all your session vars when it looks for favicon.ico but is fed a custom 404 page via RewriteRule instead without the expected 404 HTTP header. Unknown whether this is PHP only, or it affects other server-side languages. See [this page](http://stackoverflow.com/questions/2953536/) for details. – rojo Oct 31 '14 at 14:21
1

Modify the line in your .htaccess file. Just use path and filename (Without host)

ErrorDocument 404 /path/error.php

It worked for me.

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
0

You may need to tweak your existing htaccess a bit for this to work

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

as soon a RewriteRule is met, further execution is stopped by [L]. Then if the request ends with .php the error.php will be executed.

Sharky
  • 6,154
  • 3
  • 39
  • 72
0

Just add code like this in your .htaccess

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

For Example

    ErrorDocument 404 "<script>document.write(atob('PCFET0NUWVBFIGh0bWw+CjxoZWFkPgoJPG1ldGEgY2hhcnNldD0idXRmLTgiPgoJPG1ldGEgaHR0cC1lcXVpdj0iWC1VQS1Db21wYXRpYmxlIiBjb250ZW50PSJJRT1lZGdlIj4KCTxtZXRhIG5hbWU9InZpZXdwb3J0IiBjb250ZW50PSJ3aWR0aD1kZXZpY2Utd2lkdGgsIGluaXRpYWwtc2NhbGU9MSI+Cgk8IS0tIFRoZSBhYm92ZSAzIG1ldGEgdGFncyAqbXVzdCogY29tZSBmaXJzdCBpbiB0aGUgaGVhZDsgYW55IG90aGVyIGhlYWQgY29udGVudCBtdXN0IGNvbWUgKmFmdGVyKiB0aGVzZSB0YWdzIC0tPgoKCTx0aXRsZT40MDQgTm90IEZvdW5kPC90aXRsZT4KCgk8IS0tIEdvb2dsZSBmb250IC0tPgoJPGxpbmsgaHJlZj0iaHR0cHM6Ly9mb250cy5nb29nbGVhcGlzLmNvbS9jc3M/ZmFtaWx5PU1vbnRzZXJyYXQ6NTAwIiByZWw9InN0eWxlc2hlZXQiPgoJPGxpbmsgaHJlZj0iaHR0cHM6Ly9mb250cy5nb29nbGVhcGlzLmNvbS9jc3M/ZmFtaWx5PVRpdGlsbGl1bStXZWI6NzAwLDkwMCIgcmVsPSJzdHlsZXNoZWV0Ij4KCgk8c3R5bGU+CiogewogIC13ZWJraXQtYm94LXNpemluZzogYm9yZGVyLWJveDsKICAgICAgICAgIGJveC1zaXppbmc6IGJvcmRlci1ib3g7Cn0KCmJvZHkgewogIHBhZGRpbmc6IDA7CiAgbWFyZ2luOiAwOwp9Cgojbm90Zm91bmQgewogIHBvc2l0aW9uOiByZWxhdGl2ZTsKICBoZWlnaHQ6IDEwMHZoOwp9Cgojbm90Zm91bmQgLm5vdGZvdW5kIHsKIC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