6

I am trying to make 404, 500 etc pages in ErrorDocument in php .htaccess file, it works fine if give

ErrorDocument 404 http://localhost/project/errordocs/404.html

but i do not want to hard code the url here, instead i want to get the root url site name dynamically so that i do not have change it again and again as the hostname changes.

Basically i want to get this root url like: http://localhost/project can change to http://www.example1.com/project or http://www.example2.com/project etc. This url must come from projects root folder.

So that will dynamically become:

ErrorDocument 404 http://localhost/project/errordocs/404.html
ErrorDocument 404 http://www.example1.com/project/errordocs/404.html
ErrorDocument 404 http://www.example2.com/project/errordocs/404.html

Any help please?

Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33

2 Answers2

4

The asker asks incorrect. All what he writes

ErrorDocument 404 http://localhost/project/errordocs/404.html
ErrorDocument 404 http://www.example1.com/project/errordocs/404.html
ErrorDocument 404 http://www.example2.com/project/errordocs/404.html

may be done by

ErrorDocument 404 /project/errordocs/404.html

But really he want: while moving site from project folder to project1, he should not change the rule

I think it may be done by htacces placed in /project with code

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^  errordocs/404.html [L]

It will work if AllowOverride set to All (it is default).

The problem only that responce in this case will be 200 but not 404

splash58
  • 26,043
  • 3
  • 22
  • 34
  • I think you can use the flag `[R=404,L]` to set the status 404, according to the docs: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_r – Ismael Miguel Jun 07 '15 at 13:33
  • @IsmaelMiguel in this case (R=404) Apache will return standard error doc. Or user error doc setting by ErrorDocument. But the last does not take a relative path – splash58 Jun 07 '15 at 13:35
  • @IsmaelMiguel You may try to set `ErrorDocument 404 smdir/smdoc.html` (without leading slash) it produces string `smdir/smdoc.html` in browser – splash58 Jun 07 '15 at 13:39
  • According to the doc, it will make an external request and will return the error 404. But that was just an attempt to get a nice 404. Maybe a `` tag in the document will do that? I never tried it. Using php instead of html, and the function `header($_SERVER["SERVER_PROTOCOL"].' 404 Not Found');` will do the trick (Taken from http://stackoverflow.com/questions/1381123/how-can-i-create-an-error-404-in-php). – Ismael Miguel Jun 07 '15 at 13:42
  • I never used too. Using php may get the correct result. – splash58 Jun 07 '15 at 13:47
  • @Sourabh I glad that our discussion gave the result :) – splash58 Jun 07 '15 at 14:19
1

Based on the conversation you had, you would like to not have to change the path to the error document when you move your project to another folder with a different name.

Firstly, you cannot use variables when using ErrorDocument. The path that you provide must be static. The path that you specify must be either to an external URL (in which case your browser will be redirected) or to a file relative to your document root (i.e. localhost).

Unfortunately, ErrorDocument won't be able to find the file relative to the current directory (i.e. project). The only logical way of doing this would be to remove the leading slash, but this would cause Apache to render that as a string in the browser.

This brings us to the only other possible solution: mod_rewrite. The only problem with using it, however, is that it is processed early on in the mapping pipeline which may allow other modules (such as mod_proxy) to affect the process.

That said, you can try with the following:

/project/.htaccess:

RewriteEngine on

# Determine if the request does not match an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# If so, send the request to the applicable file, relative to this directory
RewriteRule ^ errordocs/404.php [L]

# Per your comment and suggested edit, add the following.
# Note: This should not make any difference as mod_rewrite and PHP should
# already handle the error document.
ErrorDocument 404 /errordocs/404.php

/project/errordocs/404.php:

This file will send the 404 header as .htaccess won't be able to do that.

<?php header("HTTP/1.0 404 Not Found"); ?>

<h1>Sorry, we couldn't find that...</h1>
<p>The thing you've requested doesn't exist here. Perhaps it flew away?</p>
Community
  • 1
  • 1
Mike Rockétt
  • 8,947
  • 4
  • 45
  • 81
  • @Sourabh - why did you make that edit? That would cause it to not work because the path must be specified from the document root (i.e. `localhost`). Please read carefully before making edits. I have tested this on my server, and it does indeed work as intended. – Mike Rockétt Jun 07 '15 at 14:05
  • you haven't specified the error document in the code which was my real problem – Sourabh Kumar Sharma Jun 07 '15 at 14:06
  • Indeed it should, but there is effectively no difference. It worked without it on my side. Will update my answer to reflect your edit anyway. – Mike Rockétt Jun 07 '15 at 14:08
  • 1
    Thanks, 1 up vote from me :), it works without even the error document. Thanks again – Sourabh Kumar Sharma Jun 07 '15 at 14:10