如果其他使用的RewriteCond模拟2级 [英] Simulating a 2-level If-Else using RewriteCond

查看:123
本文介绍了如果其他使用的RewriteCond模拟2级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我周围的RewriteCond头,要重写要么一个静态HTML页面的任何请求(如果存在),或特定的index.php(只要所请求的文件不存在)。

要说明的逻辑:

 如果HTTP_HOST是'网站(www \\)?mydomain.com
    如果文件存在:/default/static/{REQUEST_URI}.html,然后
        重写。*为/default/static/{REQUEST_URI}.html
    否则,如果文件存在:{} REQUEST_FILENAME,然后
        不重写
    其他
        重写。*为/default/index.php

我似乎没有太多的麻烦做这件事的时候我并不需要测试的HTTP_HOST。最终,这一个.htaccess文件将被处理为多个域的请求。

我知道我可以解决这个虚拟主机用的,但我想弄清楚如何做这种方式。

我不是太熟悉的一些其他标志的,将其中任何一人使用的这里(如链| C,旁边| N或跳过| S)?

在此先感谢!


更新:我已经成功地做​​到这一点,但会AP preciate选择:

 的RewriteCond%{HTTP_HOST} ^(domainA中|即doma​​inB)\\ .. * [NC]
的RewriteCond%{DOCUMENT_ROOT} /%1 /静/%{REQUEST_URI}的.html -f
重写规则(。*)? /%1/static/$1.html [NC,L]的RewriteCond%{HTTP_HOST} ^(domainA中|即doma​​inB)\\ .. * [NC]
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则。* /%1/index.php [L,QSA]


更新#2:从甘博的回答帮助,想出了另一个。我想,这将需要在添加的域的情况下,减少维修。 (感谢浓汤!)

是否有任何理由我不应该设置变量ENV?

 的RewriteCond%{HTTP_HOST} ^(domainA中|即doma​​inB)\\ .. * $ [NC]
重写规则^ - [E = APP:%1]的RewriteCond%{DOCUMENT_ROOT} /%{ENV:APP} /静态/%{REQUEST_URI}的.html -f
重写规则(。*)? /%{ENV:APP}/static/$1.html [NC,L]的RewriteCond%{} REQUEST_FILENAME!-f
重写规则。* /%{ENV:APP}/index.php [L,QSA]


解决方案

我可能会做这样的:

 的RewriteCond%{HTTP_HOST}!^(WWW \\)?例如\\ $ .COM
重写规则^ - [S = 2]的RewriteCond%{DOCUMENT_ROOT} /默认/静态%{REQUEST_URI}的.html -f
重写规则!^默认/静态/默认/静态%{REQUEST_URI}的.html [L]的RewriteCond%{} REQUEST_FILENAME!-f
重写规则!^默认/静态/默认/ index.php文件[L]

这是类似于更新的例子。除如果主机是不恰当的第一条规则将跳过以下两条规则。和重写规则模式排除开头的任何路径 /默认/静态/ 。但是,你的规则都已经pretty不错。

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]

解决方案

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.

这篇关于如果其他使用的RewriteCond模拟2级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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