htaccess custom 404 ErrorDocument relative to direcory htaccess file

Dear experts,

I am creating an custom error document for one of our clients.

We have a local development server http://localdev/websites/clientsname and offcourse the online website

I wanted to create a htaccess file which could be synchronized between both servers like a 100% filecopy.

However the ErrorDocument statement requires a full path url to direct to our custom /errordocs/404.php file.

Because both server have different path structures if would be inpossible to have the full path which satifies both servers.

One thing I know is that the errordocs are in the errordocs subdir of the directory where the .htaccess file resides.

Is there something like the below directive, or another workaround?
ErrorDocument 404 %{currentWorkingDir}/errordocs/404.php

I have read through the apache manual, and searched on EE but didn't find an answer.

Kind regards

LVL 49
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The answer is no, you can't do it the way you want.

Let me elaborate. Let's say you have paths of your projects like this

ErrorDocument 404 /root_dir/project_dir1/errordocs/404.php
ErrorDocument 404 /root_dir/project_dir2/errordocs/404.php
ErrorDocument 404 /root_dir/project_dir3/errordocs/404.php

So you wan't to get dynamicaly "/root_dir/project_dir3" part. Imagine if someone enteres url that doesn't exist.

or maybe

as you can see you canno't "tell" htaccess that should look only for "/root_dir/project_dir3" or "/root_dir/project_dir2" because there is no way you could tell what would "%{currentWorkingDir}" be.

To overcome this problem is for you to open port based virtual hosts in your test server.

So you would avoid subdirs for projects.  http://localdev/websites/clientsname would become  http://localdev:3001/
Then you could use ErrorDocument 404 /errordocs/404.php path on your test and client server.

Hi Roonaan,
I am not sure if there is any facility for specifying the error document path via environment variables. But you could takle this in 2 different ways.

a. Have a redirect script (cgi-perl file) and then have the errordocument directive point to that redirect cgi script. This way you can have more control on what and where you want to redirect the user.
b. In one or both the webservers create the virtual directory (need to be specified in httpd.conf if it is apache) for /errordocs. That way, wherever the /errordocs directory is it will always be accessible from the root.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RoonaanAuthor Commented:
>as you can see you canno't "tell" htaccess that should look only for "/root_dir/project_dir3"
>or "/root_dir/project_dir2" because there is no way you could tell what would "%{currentWorkingDir}" be.

But htaccess doesn't "know" where the htaccess file itself is localized on the server? Because htaccess is always in the root of my "website" (devserver/websites/clientname/.htaccess ||

Even though the 404 should occur in devserver/websites/clientname/somedir/someotherdir/somenonexistentfile, the htaccess would still be in the same location wouldn't it be?


Problem is that there is no variable that you could use to dynamicaly set path to htaccess file (so that he would now its location). All variables that are available to htaccess are those for file that "triggered" lets say 404 error. So you would have variable for that missing file and not htaccess..

you can see all variables here.

I would say switch to port based virtual hosts for testing purposes. This is also good for PHP scripts aswell (document_root variable..).

RoonaanAuthor Commented:
I think I'll go with an /errordocs/404.php which then redirects to the correct development site, based on the information from apache. At the live server it then would redirect straight to the correct 404 because the site is at root.

Thank you all.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.