开发者

Using htaccess rewrite to make a subdirectory be its own root for root relative path requests

开发者 https://www.devze.com 2023-01-02 15:58 出处:网络
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...

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 开发者_StackOverflowout 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消