Apache ErrorDocument External Redirect with Variables
Asked Answered
P

4

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}}
Palladic answered 7/6, 2012 at 20:13 Comment(1)
Where {{REDIRECT_URL}} is the requested url.Palladic
T
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

Thusly answered 17/2, 2015 at 15:1 Comment(0)
W
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
Wavellite answered 27/2, 2014 at 9:56 Comment(1)
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/.Spates
C
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.

Cheyennecheyne answered 16/1, 2017 at 19:44 Comment(0)
C
-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
Cupulate answered 7/6, 2012 at 20:45 Comment(1)
That does not add the requested url ... I thought I was clear in my question.Palladic

© 2022 - 2024 — McMap. All rights reserved.