智能目录切换.htaccess [英] Smart directory switching .htaccess

查看:86
本文介绍了智能目录切换.htaccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





假设我有x.com和y.com。在z.com上,我有一个名为x.com/的目录,另一个名为y.com/。我有一个CNAME记录映射x.com和y.com到z.com。



我想要实现的是这个;



在其中一个终端域上访问文件;

x.com/test.txt



这应该映射到;

z.com/x.com/test.txt



我想到的方法是:



Hi,

Let's say I have x.com and y.com. On z.com, I have a directory called x.com/ and another called y.com/. I have a CNAME record mapping x.com and y.com to z.com.

What I want to achieve is this;

A file is accessed on one of the end domains;
x.com/test.txt

This should then map to;
z.com/x.com/test.txt

The way I thought of going about this is:

RewriteCond %{REQUEST_URI} !^https:\/\/z\.com\/switch.php\?.+ [NC]
RewriteRule ^(.*) https://z.com/switch.php?f=$1 [R=301,NE,L]



然后我会使用switch.php回显$ _SERVER ['HTTP_HOST'] / file.txt,其中文件路径在$ _GET ['f']中指定,由.htaccess放置。



.htaccess不仅会导致无限循环,而且我相信在没有switch.php的情况下只能在.htaccess中执行此操作。循环是因为正则表达式无法排除正在重写的文件。我想这是因为$ 1在正则表达式中没有捕获组,但是我不知道如何在具有否定的正则表达式的情况下创建捕获组。我需要说'如果它不是switch.php?什么,将$ 1设置为主机名之后的任何内容'。



添加此规则会停止无限循环但不会重写发生:




I would then use switch.php to echo $_SERVER['HTTP_HOST']/file.txt where the filepath is specified in $_GET['f'], placed there by .htaccess.

Not only does the .htaccess cause an infinite loop, but I believe it's possible to do this just in .htaccess without switch.php at all. The loop is because the regex isn't working to exclude the file being rewritten to. I'd imagine this is because the $1 has no capture group in the regex, but I have no idea how to create a capture group while having a negated regex. I'd need to say 'if it's not switch.php?anything, set $1 to whatever's after the hostname'.

Adding this rule stops the infinite loop but no rewriting happens:

RewriteCond %{REQUEST_URI} ^https:\/\/z\.com\/(.+) [NC]



如果你能帮我解决这个问题,我真的很感激。



我尝试了什么:



所有提到的问题,加上大量的谷歌搜索


If you could help me to fix this I'd really appreciate it.

What I have tried:

Everything mentioned in question, plus a lot of googling

推荐答案

1 [R = 301,NE,L]
1 [R=301,NE,L]



然后我会使用switch.php来回显


I would then use switch.php to echo


_SERVER ['HTTP_HOST'] / file.txt其中文件路径在
_SERVER['HTTP_HOST']/file.txt where the filepath is specified in


_GET ['f']中指定,由.htaccess放置。



.htaccess不仅导致无限循环,而且我相信在没有switch.php的情况下只能在.htaccess中执行此操作。循环是因为正则表达式无法排除正在重写的文件。我想这是因为
_GET['f'], placed there by .htaccess.

Not only does the .htaccess cause an infinite loop, but I believe it's possible to do this just in .htaccess without switch.php at all. The loop is because the regex isn't working to exclude the file being rewritten to. I'd imagine this is because the


这篇关于智能目录切换.htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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