External redirect Apache ErrorDocument with variables

Something similar is possible in Apache (for example, redirecting to an external URL with a path that causes a 403 error)

ErrorDocument 403 http://www.sample.com/{{REDIRECT_URL}} 
+6
source share
4 answers

I am creating / Publishing a directory, but I want this to be served by the main index.php, where there are files in this directory.

/ Publication? page = 1 will be served /index.php,/Publication/file.pdf - this is a file located in this directory.

Apache returend 403 error because the directory / publication is not allowed to be listed. You redirect the 403 error to /index.php, you cannot intercept catch variables. I think you can use the REDIRECT_QUERY_STRING variable, but this can ruin my php classes.

I changed the configuration of mod_rewrite to search directories, see "#", removed the ErrorDocument 403 directive, as it is not necessary.

 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f # RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^(.*)$ index.php?path=$1 [L,QSA] </IfModule> 

What i have

 /Publication > /index.php?path=Publication /Publication/?page=1 > /index.php?path=Publication&page=1 /Publication/file.pdf > /Publication/file.pdf 
0
source

ErrorDocument configuration option, unfortunately, does not support the extension of server variables. You can try using a local script that will give you a redirect.

 ErrorDocument 403 /cgi-bin/error.cgi 

Please note that the script must be local, otherwise you will not receive the passed variables REDIRECT_* . And in the script itself, you can issue a redirect command:

 #!/usr/bin/perl my $redirect_status = $ENV{'REDIRECT_STATUS'} || ''; my $redirect_url = $ENV{'REDIRECT_URL'} || ''; if($redirect_status == 403) { printf("Location: http://%s%s\n", 'www.sample.com', $redirect_url); printf("Status: %d\n", 302); } print "\n"; 

See the Apache documentation for more information http://httpd.apache.org/docs/2.4/custom-error.html

0
source

Yes! But only starting with Apache 2.4.13:

From 2.4.13, expression syntax can be used inside the directive to create dynamic strings and URLs.

(from https://httpd.apache.org/docs/2.4/mod/core.html#errordocument )

The following configuration will result in an HTTP 302 response:

 ErrorDocument 403 http://www.example.com%{REQUEST_URI} 

Note the absence of a slash, because %{REQUEST_URI} begins with a slash.

0
source

I guess this is possible, as the doc says. http://httpd.apache.org/docs/2.0/mod/core.html#errordocument

Maybe so:

 ErrorDocument 403 http://www.sample.com/?do=somthing 
-1
source

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