使用链接 RewriteRule 中的 RewriteCond 检查文件是否存在

发布于 2024-08-29 18:55:15 字数 651 浏览 2 评论 0原文

我希望你能在这里帮助我。

我有一个 RewriteRule,它根据域名为您提供一个 crossdomain.xml 文件。我以忽略“.dev”的方式拥有它。中间的字符串,例如:

Request: http://site1.dev.mydomain.com/crossdomain.xml
返回文件:/etc/httpd/conf/crossdomain/site.mydomain.com.xml

RewriteCond %{HTTP_HOST} "^(.*)\.dev\.(.*)"
RewriteCond %{REQUEST_URI} "/crossdomain.xml"
    RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
    RewriteRule ^(.*)\.dev\.([^/]*)/crossdomain.xml /etc/httpd/conf/crossdomain/$1.$2.xml [L]

问题:如何检查文件是否存在?我尝试将其添加到顶部,但它不起作用:

RewriteCond  /etc/httpd/conf/crossdomain/$1.$2.xml  -f

我想因为这是一个连锁规则,不知道。请帮忙。

I hope you could help me here.

I have a RewriteRule which gives you a crossdomain.xml file depending on the domain name. I have it in a way that ignores the ".dev." string in the middle, example:

Request: http://site1.dev.mydomain.com/crossdomain.xml
Returns file: /etc/httpd/conf/crossdomain/site.mydomain.com.xml

RewriteCond %{HTTP_HOST} "^(.*)\.dev\.(.*)"
RewriteCond %{REQUEST_URI} "/crossdomain.xml"
    RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
    RewriteRule ^(.*)\.dev\.([^/]*)/crossdomain.xml /etc/httpd/conf/crossdomain/$1.$2.xml [L]

Question: How do I check if the file exists? I tried adding this on top but it doesn't work:

RewriteCond  /etc/httpd/conf/crossdomain/$1.$2.xml  -f

I guess because it is a chained rule, no idea. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七色彩虹 2024-09-05 18:55:15

是的,我刚刚找到了解决方法,我所做的不是终止重写并添加另一个重写,该重写仅在第一个应用时才起作用。它检查文件是否不存在,如果存在,则应用进一步重写:

RewriteCond %{HTTP_HOST} "^(.*)\.dev\.(.*)"
RewriteCond %{REQUEST_URI} "/crossdomain.xml"
    RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
    RewriteRule ^(.*)\.dev\.([^/]*)/crossdomain.xml /etc/httpd/conf/crossdomain/$1.$2.xml

RewriteCond /etc/httpd/conf/crossdomain/$1.xml !-f
        RewriteRule /etc/httpd/conf/crossdomain/(.*)\.xml /etc/httpd/conf/crossdomain/crossdomain-default.xml [L]

Right, I just found a way around it, what I do is not terminating the Rewrite and add another one which is meant to work only if the first applied. It checks if the file doesn't exists, if so it applies a further rewrite:

RewriteCond %{HTTP_HOST} "^(.*)\.dev\.(.*)"
RewriteCond %{REQUEST_URI} "/crossdomain.xml"
    RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
    RewriteRule ^(.*)\.dev\.([^/]*)/crossdomain.xml /etc/httpd/conf/crossdomain/$1.$2.xml

RewriteCond /etc/httpd/conf/crossdomain/$1.xml !-f
        RewriteRule /etc/httpd/conf/crossdomain/(.*)\.xml /etc/httpd/conf/crossdomain/crossdomain-default.xml [L]
御弟哥哥 2024-09-05 18:55:15

我认为这与链接的规则没有任何关系,您只是在与错误的值进行比较。

RewriteCond %{REQUEST_FILENAME} -f

应该有效。

I don't think it has anything to do with the rule being chained, you're just comparing against the wrong value.

RewriteCond %{REQUEST_FILENAME} -f

should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文