使用 RewriteCond 模拟 2 级 If-Else

发布于 2024-09-01 16:35:06 字数 1360 浏览 2 评论 0原文

我正在尝试了解 RewriteCond,并希望将任何请求重写到静态 html 页面(如果存在)或特定的 index.php(只要请求的文件不存在)。

为了说明逻辑:

if HTTP_HOST is '(www\.)?mydomain.com'
    if file exists: "/default/static/{REQUEST_URI}.html", then
        rewrite .* to /default/static/{REQUEST_URI}.html
    else if file exists: {REQUEST_FILENAME}, then
        do not rewrite
    else
        rewrite .* to /default/index.php

当我不需要测试 HTTP_HOST 时,我似乎没有遇到太多麻烦。最终,这一 .htaccess 文件将处理多个域的请求。

我知道我可以通过虚拟主机解决这个问题,但我想弄清楚如何做到这一点。

我对其他一些标志不太熟悉,它们在这里有用吗(如 chain|C、next|N 或skip|S)?

提前致谢!


更新:我已经设法做到了,但希望有其他选择:

RewriteCond %{HTTP_HOST} ^(domainA|domainB)\..* [NC]
RewriteCond %{DOCUMENT_ROOT}/%1/static/%{REQUEST_URI}.html -f
RewriteRule (.*)? /%1/static/$1.html [NC,L]

RewriteCond %{HTTP_HOST} ^(domainA|domainB)\..* [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /%1/index.php [L,QSA]

更新#2:在Gumbo答案的帮助下,想出了另一个。我喜欢的是,在添加域的情况下,这将需要更少的维护。 (谢谢 Gumbo!)

我不应该设置 ENV 变量有什么原因吗?

RewriteCond %{HTTP_HOST} ^(domainA|domainB)\..*$ [NC]
RewriteRule ^ - [E=APP:%1]

RewriteCond %{DOCUMENT_ROOT}/%{ENV:APP}/static/%{REQUEST_URI}.html -f       
RewriteRule (.*)? /%{ENV:APP}/static/$1.html [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /%{ENV:APP}/index.php [L,QSA]

I'm trying to get my head around RewriteCond, and want to rewrite any requests either to a static html page (if it exists), or to a specific index.php (so long as the requested file doesn't exist).

To illustrate the logic:

if HTTP_HOST is '(www\.)?mydomain.com'
    if file exists: "/default/static/{REQUEST_URI}.html", then
        rewrite .* to /default/static/{REQUEST_URI}.html
    else if file exists: {REQUEST_FILENAME}, then
        do not rewrite
    else
        rewrite .* to /default/index.php

I don't seem to have much trouble doing it when I don't need to test for the HTTP_HOST. Ultimately, this one .htaccess file will be handling requests for several domains.

I know I could get around this with vhosts, but I'd like to figure out how to do it this way.

I'm not too familiar with some of the other flags, will any of them be of use here (like chain|C, next|N or skip|S)?

Thanks in advance!


UPDATE: I've managed to do it, but would appreciate alternatives:

RewriteCond %{HTTP_HOST} ^(domainA|domainB)\..* [NC]
RewriteCond %{DOCUMENT_ROOT}/%1/static/%{REQUEST_URI}.html -f
RewriteRule (.*)? /%1/static/$1.html [NC,L]

RewriteCond %{HTTP_HOST} ^(domainA|domainB)\..* [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /%1/index.php [L,QSA]

UPDATE #2: With help from Gumbo's answer, came up with another. I like that this would would require less maintenance in the case of added domains. (Thanks Gumbo!)

Are there any reasons why I shouldn't set ENV variables?

RewriteCond %{HTTP_HOST} ^(domainA|domainB)\..*$ [NC]
RewriteRule ^ - [E=APP:%1]

RewriteCond %{DOCUMENT_ROOT}/%{ENV:APP}/static/%{REQUEST_URI}.html -f       
RewriteRule (.*)? /%{ENV:APP}/static/$1.html [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /%{ENV:APP}/index.php [L,QSA]

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

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

发布评论

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

评论(1

生生不灭 2024-09-08 16:35:06

我可能会这样做:

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$
RewriteRule ^ - [S=2]

RewriteCond %{DOCUMENT_ROOT}/default/static%{REQUEST_URI}.html -f
RewriteRule !^default/static/ default/static%{REQUEST_URI}.html [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^default/static/ default/index.php [L]

这与您更新的示例类似。只不过如果主机不合适的话,第一条规则会跳过后面两条规则。 RewriteRule 模式排除以 /default/static/ 开头的任何路径。但你的规则已经很好了。

I would probably do it like this:

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$
RewriteRule ^ - [S=2]

RewriteCond %{DOCUMENT_ROOT}/default/static%{REQUEST_URI}.html -f
RewriteRule !^default/static/ default/static%{REQUEST_URI}.html [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^default/static/ default/index.php [L]

This is similar to your updated example. Except that the first rule will skip the following two rules if the host is not appropriate. And the RewriteRule patterns exclude any path that starts with /default/static/. But your rules are already pretty good.

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