Forum Moderators: phranque
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.
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
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...]
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://([^.]+\.)?example\.com [NC]
RewriteRule ^(.*)/[^/]+/[^.]+\.xls$ http://www.example.com/$1/ [NC,R=302,L]
Jim