views:

60

answers:

1

Can I use htaccess to capture requests from a certain subdirectory and make that directory use itself as the root directory for any root relative path requests? For example if I have...

http://www.example.com/subFIXED/subANY/restofpath

...where subFIXED is always the same directory, subANY is any immediate subdirectory of subFIXED, and I want a redirection of all href/src requests from any file under subANY to use subANY as the 'root' (sort of like a subdomain), in effect having root level requests use this as the root directory level:

http://www.example.com/subFIXED/subANY/

Instead of this:

http://www.example.com/

I'm assuming I can put an htaccess file in subFIXED to handle all calls coming from anything under any subANY, but not being very familiar with htaccess rewriting, variables, etc., I can't figure out how to capture which subANY directory is making the root level request and then use that capture to make a rewrite to consider that directory the root level of any root relative path requests from it.

Thanks for your help

+2  A: 

Euhm, .htaccess? It would have to rely on the extremely unreliable HTTP_REFERER, no thanks.

Add a <base> element in your HTML & be done. http://www.w3schools.com/tags/tag_base.asp


Edit: relative to root ("/foo") should also be accounted for, so after fixing relative paths with <base>:

.htaccess (extremely unreliable because switching of subpath is nigh impossible, and HTTP_REFERER's are extremely unreliable. in short: don't use)

RewriteCond %{HTTP_REFERER} ^.*://[^/]+/subFIXED/([^/]+)/
RewriteCond %{REQUEST_URI}  !^/subFIXED
RewriteRule ^(.*)$ /subFIXED/%1/$1 [R=301,L,QSA] //drop the R=301 if POSTing, but url will not show the 'correct' one in that case

Pitfalls:

  1. POSTing is awkward (cannot redirect & preserve POST, so will have to land 'normally', which will make the REFERER on subsequent requests useless / false
  2. Referer is often not sent
  3. Switching to other 'subAny' from any other 'subAny' is impossible when sending REFERER

More viable solutions:

  1. Postprocess each and every request with script on the server, adding a dot (.) to every reference (href/src) that starts with '/'.
  2. The previous one can be done with clientside javascript, but this is not recommendable because all links for searchbots & browsers with js disabled would break
  3. Make an actual subdomain, this is just a hassle.

In short, no desirable solutions except for making actual subdomains. What is the exact problem you're trying to solve that you need this 'fixed rootpath' solution? There could be others that don't involve this much hassle.

Wrikken
I had never seen that tag before -- awesome. You don't know of a way to make that only apply to images do you?
Kerry
Unfortunately, from my understanding of the <base> element it does not redirect root relative paths (just relative paths). So that is not an option.
Scott
Pity, would be an easy fix, alternatives on their way...
Wrikken
1) Thank you for your time on this. 2) Making a subdomain is not an option at this time. 3) Switching between 'subANY's nor SEO is a concern (see below). Question: even though it is a request coming from a file on my domain, the http_referer is still unreliable?To answer you, I want to send a client a link to the index of their subANY for them to review aspects of a website design. I want their navigation, css, images, etc. (root relative) links, to work within their 'subdomain.' Each setup is temporary, and subANY's will change names (base on client). Is there perhaps a PHP solution?
Scott
I just tested your code, and the good news is I got through to the page (unlike some of my earlier attempts at my own rewrites); the bad news is that it did not work (my test is a link tag to a css file under the subANY folder with a root path of '/css/Style.css' that should change the color of my text if the redirect is working).
Scott
Wrikken
Every HTML page is not necessarily using PHP. I was thinking of perhaps something more like a PHP file that I send the client to, which loads the regular index page and outputs it with the '.' appended to all root links for images, css, etc., and then have the php catch any requests to navigate within the 'subdomain' and redirect that back to itself to serve up the new page likewise. The url would never change, but that is okay for the review. The only problem I see with it is that any included files, or any background images in the css files that have root level paths would still be broken.
Scott
Wrikken
As I've been pondering this, I tend to agree... too much work. I think I am going to forget the idea for now. I believe at some future point a subdomain will be possible, and as you said above, that's really they way to do it. Thanks for your time, thoughts, and help. If nothing else, I learned some things through it all.
Scott