Forum Moderators: phranque

Message Too Old, No Replies

If not direct referral, go to this file one folder up

structuring .htaccess to redirect to one folder up not root

         

Jeremy_H

6:39 am on Jan 20, 2006 (gmt 0)

10+ Year Member



Hello,

I'm very inexperienced with regular expression and mod_rewrite and I'm trying to learn it (trial by fire apparently) and fix my .htacess script at the same time.

This is what a section of my script looks like now:

RewriteCond %{HTTP_REFERER}!^http://[^/]*domain\.com [NC]
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{REQUEST_URI} [^/](.*\.xls)$ [NC]
RewriteRule ^(.*) / [L,R]
#RewriteRule ^(.*) /?doc=%1

This is my understanding of what this says:

"Here are the rules. Look at the variable of the referring website. If it is NOT from anything.domain.com (not case sensitive), AND if the referring website is not blank (from a direct request), AND the requesting file is anything.xls (not case sensitive), THEN send the user to the root file."

While this is great, I've since restructured my website, and now I want to send the user to the folder before the folder containing the .xls file. Not the root anymore. I thought I could change this by substituting the "/" on the last two lines with "../", which would say, one folder up, instead of root.

The code would look like this:

RewriteCond %{HTTP_REFERER}!^http://[^/]*domain\.com [NC]
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{REQUEST_URI} [^/](.*\.xls)$ [NC]
RewriteRule ^(.*) ../ [L,R]
#RewriteRule ^(.*) ../?doc=%1

However a .xls request to the file from another domain and not direct gives the user a 400 Bad Request error, instead of sending the user to the folder above the one containing the .xls file.

What am I doing wrong?

Any help would be greatly appreciated. Thanks.

jdMorgan

4:38 pm on Jan 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Folders above Document_Root are not accessible via HTTP on any properly-configured server. To allow this would open up the server to all kinds of security/privacy problems.

Therefore, you cannot generally rewrite or redirect to any directory above Document_Root.

Other solutions involve using a script to 'include' that content using a *file* request, or using *nix symlinks in the filesystem or Alias directives in httpd.conf to make the needed files appear to reside in the HTTP-accessible directory.

Jim

Jeremy_H

5:15 pm on Jan 20, 2006 (gmt 0)

10+ Year Member



Thanks for your reply jdMorgan,

I'm sorry for the confusion, let me clarify.

Say all the files are in a folder like the following structures:

[domain.com...]
[domain.com...]
[domain.com...]

Right now if there was a direct request for one of these files, the user would be redirected to:

[domain.com...]

Instead, I would like the user to be redirect to two (I said one before, but now I realize it's two) folders up from the xls file.

[domain.com...] GOES TO
[domain.com...] NOT
[domain.com...]

[domain.com...] GOES TO
[domain.com...] NOT
[domain.com...]

[domain.com...] GOES TO
[domain.com...] NOT
[domain.com...]

jdMorgan

10:42 pm on Jan 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Based on your examples above, I'd recommend:

RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://([^.]+\.)?example\.com [NC]
RewriteRule ^(.*)/[^/]+/[^.]+\.xls$ http://www.example.com/$1/ [NC,R=302,L]

Note that this is a 302-Found (Moved Temporarily) redirect, as was your original code. I just made the redirect type explicit in the code, so it stands out.

Jim

Jeremy_H

3:39 pm on Jan 22, 2006 (gmt 0)

10+ Year Member



Thank you very much, these modifications work great. It just goes to show that I have much to learn to even have a working knowledge of regular expression, as my approach was completely wrong.

Does anybody know of a good book they would recommend for learning regular expression?

Thanks