Get the dynamic name of the root site node .htaccess

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

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

but I donโ€™t want to hardcode the URL here, instead I want to get the root URL name dynamically so that I donโ€™t have to change it again and again when changing the host name.

Basically, I want to get this root url as: http://localhost/project can change to http://www.example1.com/project or http://www.example2.com/project , etc. This URL must come from the root of the projects folder.

So this will be dynamic:

 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?

+6
source share
2 answers

The questioner asks incorrectly. All that 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 

can run

 ErrorDocument 404 /project/errordocs/404.html 

But actually he wants: moving a site from the project folder to project1, he should not change the rule

I think this can be done using 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 is set to All (default).

The only problem is that the answer in this case will be 200, but not 404

+4
source

Based on the conversation you had , you will not need to change the path to the document with an error when moving the project to another folder with a different name.

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

Unfortunately, ErrorDocument cannot find the file relative to the current directory (i.e. project ). The only logical way to do this is to remove the leading slash, but this will cause Apache to display it as a string in the browser.

This leads us to the only possible solution: mod_rewrite . However, the only problem with its use is that it is processed at an early stage in the mapping pipeline , which may allow other modules (such as mod_proxy) to influence the process .

However, you can try 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 a 404 header because .htaccess cannot do this.

 <?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> 
+1
source

Source: https://habr.com/ru/post/988645/


All Articles