7

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}}
Pykler
  • 14,565
  • 9
  • 41
  • 50

4 Answers4

1

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

Timur Bakeyev
  • 296
  • 4
  • 4
0

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
nerkn
  • 1,867
  • 1
  • 20
  • 36
  • Where is your `.htaccess`? Is it inside the `/Publication` dir? If so, do you have `DirectorySlash Off` somewhere in your `.htaccess`? Otherwise, how can you say that `/Publication > /index.php?path=Publication` when by default Apache will redirect `/Publication` to `/Publication/` if `DirectorySlash On` is set and then rewrite rules will rewrite `/Publication/ > /index.php?path=Publication/`. I'm asking you just because I am curious about your setup and I would like to map let's say `/Publication` directly to `/index.php?path=Publication` without letting Apache redirect to `/Publication/`. – tonix May 08 '16 at 17:59
0

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.

Michael
  • 776
  • 1
  • 11
  • 20
-1

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
Starx
  • 77,474
  • 47
  • 185
  • 261
mimiz
  • 2,178
  • 2
  • 14
  • 14