使用htaccess重写来使一个子目录成为其根目录相对路径请求的根目录 [英] Using htaccess rewrite to make a subdirectory be its own root for root relative path requests

查看:191
本文介绍了使用htaccess重写来使一个子目录成为其根目录相对路径请求的根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用htaccess来捕获来自某个子目录的请求,并将该目录用作任何根相对路径请求的根目录吗?例如,如果我有...

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

...其中subFIXED始终是相同的目录,subANY是subFIXED的任何直接子目录,我希望重定向在subany下的任何文件中使用subany作为'root'(有点像子域)的所有href / src请求中的所有href / src请求,实际上有根级请求使用它作为根目录级别:

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

而不是:

  http://www.example.com/ 

我假设我可以在subFIXED中放置一个htaccess文件来处理来自任何subany下任何东西的所有调用,但不能对htaccess重写,变量等非常熟悉,我无法弄清楚如何捕获哪个subany目录正在创建根级别请求,然后使用该捕获进行重写以将该目录视为r或者任何根相对路径请求的级别。



感谢您的帮助

解决方案

div>

Euhm,.htaccess?它必须依赖极端不可靠的HTTP_REFERER,不用多谢。



在HTML& HTML中添加一个< base>元素。做完了。
http://www.w3schools.com/tags/tag_base.asp

$ b编辑:相对于根(/ foo)也应该考虑在内,所以在修复相对路径后< base>:



.htaccess(非常不可靠,因为子路径的切换几乎不可能,而HTTP_REFERER是非常不可靠的,总之:不要使用)

  RewriteCond%{HTTP_REFERER} ^。*:// [^ /] + / subFIXED /([^ /] +)/ 
RewriteCond%{REQUEST_URI}!^ / subFIXED
RewriteRule ^(。*)$ / subFIXED /%1 / $ 1 [R = 301,L,QSA] //如果发布则丢弃R = 301,在这种情况下不会显示'正确的'

陷阱:


  1. POSTing很尴尬(无法重定向和保留POST,因此必须正常登录,这会使后续请求上的REFERER无效/ false

  2. Referer通常不会被发送

  3. 在发送REFERER时,切换到其他任何'subAny'的其他'subAny'是不可能的

更可行的解决方案:


  1. 使用脚本在服务器上对每个请求进行后处理,为以'/'开头的每个引用(href / src)添加一个点(。)。 / li>
  2. 上一个可以用客户端的javascript来完成,但这并不值得推荐,因为searchbots& js禁用的浏览器会中断

  3. 创建实际的子域名,这只是一个麻烦。
  4. >总之,除了制作实际的子域名之外,没有可取的解决方案。您想要解决的具体问题是什么,您需要这种固定根路径解决方案?可能有其他人不涉及这么多麻烦。


    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

    解决方案

    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.

    这篇关于使用htaccess重写来使一个子目录成为其根目录相对路径请求的根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆