Reputation: 527
I am working on an open source project. I do not know what directory this project will be installed in on the end user. I need to add rules to the .htaccess file created for my project, so that 404's and other point to a specific file, in the root of my project. The setup script is called in a folder called "setup", in relation to the root folder like such root/setup/index.php.
The path I use to direct 404's from the .htaccess in the root to errorcode.php (also in the root folder) is this:
$installURL = "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']);
$save= $installURL;
$str = str_replace( '/setup', '', $save ); //remove /setup/
$installURL= $str ;
I then open the .htaccess file and make my rules like such:
$fp = fopen("../.htaccess", "a");
fwrite($fp, "\n\n# ErrorDocuments \n");
fwrite($fp, "ErrorDocument 400 ". $installURL."/errorcode.php?error=400 \n");
There's a list of rules for all of the 400 HTTP headers.
This has worked for me for about a month now. Today, I started getting server errors, and the log file says the following:
[Thu Oct 04 11:56:59 2012] [notice] cannot use a full URL in a 401 ErrorDocument directive --- ignoring!
[Thu Oct 04 11:56:59 2012] [alert] [client 127.0.0.1] C:/wamp/www/Project-BlackHawk/.htaccess: Unsupported HTTP response code 428, referer: http://localhost/Project-BlackHawk/setup/index.php
My question is, am I supposed to use something like this for the path to ErrorDocument:
$installURL = dirname($_SERVER['SCRIPT_NAME']);
$save= $installURL;
$str = str_replace( '/setup', '', $save ); //remove /setup/
$installURL= $str ;
Upvotes: 2
Views: 106
Reputation: 143906
It sounds like you need to remove the http://localhost
part from your ErrorDocument
directive. So $_SERVER['SCRIPT_NAME']
would probably do that for you, so would $_SERVER['REQUEST_URI']
.
Upvotes: 2