Avatar of xfvgdrthbdtyvhgscv
xfvgdrthbdtyvhgscv
 asked on

Htaccess redirect depending on referrer?

Hello all,

Could someone please tell me what I need in an htaccess file to do the following:

On an Apache server, I have sub-folder-A :

www.mydomain.com/folder/sub-folder-A

in which there are a bunch of PDF files

www.mydomain.com/folder/sub-folder-A/pdf_1.pdf
www.mydomain.com/folder/sub-folder-A/pdf_2.pdf
..etc..

and an index shtml page that provides links to all the pdf files.  

www.mydomain.com/folder/sub-folder-A/index.shtml

I want to restrict access and redirect so that  acessing any file in sub-folder-A from any link on any page of mydomain.com, (including the links on www.mydomain.com/folder/sub-folder-A/index.shtml), causes a redirect to

www.myotherdomain.com/cgi-bin/myscript.pl

myscript.pl will do some logging, record the url that was requested, then redirect the user back to the requested url (so accessing the file from myscript.pl must NOT result in a redirect)

Trying to access any file in sub-folder-A by any other route defaults to "Forbidden".

Thanks.
Apache Web Server

Avatar of undefined
Last Comment
caterham_www

8/22/2022 - Mon
Michael Worsham

It can be done, however, what you are asking for cannot be done with .htaccess. You need to have access to the httpd.conf as it will require RewriteMap to call an external Perl script.

Example...

RewriteEngine on
RewriteCond %{REQUEST_URI} ^http://www\.mydomain\.com/folder/sub-folder-A/ [NC]
RewriteMap log prg:/path/to/cgi-script/myscript.pl
RewriteRule ^.*$ /denied.html [F]
ASKER CERTIFIED SOLUTION
caterham_www

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
xfvgdrthbdtyvhgscv

ASKER
Caterham,

That certainly is succinct, thanks.

To summarize my understanding:
IF (HTTP_REFERER is not equal to http://www.myotherdomain.com/cgi-bin/myscript.pl) THEN
   redirect to   http://www.myotherdomain.com/cgi-bin/myscript.pl

I'm unclear on what the \.pdf$ is for, though.

Reminder in case it's significant - the same thing applies to the shtml index page in that folder.

Thanks.
caterham_www

> IF (HTTP_REFERER is not equal to http://www.myotherdomain.com/cgi-bin/myscript.pl) THEN
   redirect to   http://www.myotherdomain.com/cgi-bin/myscript.pl

> I'm unclear on what the \.pdf$ is for, though.

I guessed requests for the html page /folder/sub-folder-A/index.shtml should be served and not redirected?

> Reminder in case it's significant - the same thing applies to the shtml index page in that folder.

So the index page should be served only with a referer from http://www.myotherdomain.com/cgi-bin/myscript.pl, too? In that case substitute \.pdf$ with ^
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
xfvgdrthbdtyvhgscv

ASKER
>> So the index page should be served only with a referer from http://www.myotherdomain.com/cgi-bin/myscript.pl, too? In that case substitute \.pdf$ with ^

Yes, that's right.  Any access of any file, including the shtml index file, redirects to the Perl script.  So, OK, I replace \.pdf$ with ^ - what's it do?
caterham_www

^ matches each request
xfvgdrthbdtyvhgscv

ASKER
OK, thanks - points awarded.

But if you have a minute, I'm sorry but I'm still not entirely clear on "matches each request" - I mean, we did our matching in the RewriteCond line, where we said "if the referer is not X".  What more matching is needed?

Thanks again.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
xfvgdrthbdtyvhgscv

ASKER
No, wait, I think I get it - the RewriteRule is a search-and-replace regex.

Replace ^ with X

Where ^ matches the entire requested uri.

Yes?

Michael Worsham

Here is also a good site to visit for help on ReWrite under Apache...

http://www.modrewrite.com
caterham_www

> Where ^ matches the entire requested uri.

Yes, in your subfolder the string index.shtml or foo.pdf would be matched (everything left from th path is striped since you're in /some/folder-A/).
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes