Pykler
Pykler

Reputation: 14865

Apache ErrorDocument External Redirect with Variables

Is something like this possible in Apache ... (i.e. redirect to an external url with the path causing the 403 error passed along)

ErrorDocument 403 http://www.sample.com/{{REDIRECT_URL}}

Upvotes: 7

Views: 4596

Answers (4)

Michael
Michael

Reputation: 816

Yes! But only starting with Apache 2.4.13:

From 2.4.13, expression syntax can be used inside the directive to produce 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 lack of a slash because %{REQUEST_URI} starts with a slash.

Upvotes: 0

Timur Bakeyev
Timur Bakeyev

Reputation: 438

ErrorDocument configuration option, unfortunatelly, doesn't support server variables expansion. What you can try is to use local script, that will issue redirect for you.

ErrorDocument 403 /cgi-bin/error.cgi

Note, that script has to be local, otherwise you won't get REDIRECT_* variables passed. And in the script itself you can issue redirect statement:

#!/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 Apache documentation for more insights http://httpd.apache.org/docs/2.4/custom-error.html

Upvotes: 1

nerkn
nerkn

Reputation: 1980

I create /Publication directory, but I want this will be served by main index.php where as there are file in this directory.

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

Apache returend 403 error since /Publication directory has no permission to be listed. Whe you redirect 403 error to /index.php you cant catch variables. I think REDIRECT_QUERY_STRING variable can be used but that would mess my php classes.

I changed mod_rewrite config to catch directory serving, see '#', removed ErrorDocument 403 directive, since no need.

<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

Upvotes: 0

mimiz
mimiz

Reputation: 2288

I assume it's possible as the doc say .. http://httpd.apache.org/docs/2.0/mod/core.html#errordocument

Maybe like that :

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

Upvotes: -1

Related Questions