小编Hen*_* L.的帖子

使用RewriteCond模拟2级If-Else

我试图了解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
Run Code Online (Sandbox Code Playgroud)

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

我知道我可以用vhosts来解决这个问题,但是我想知道如何这样做.

我不太熟悉其他一些标志,它们中的任何一个都可以使用(比如链| C,下一个| N或跳过| 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]
Run Code Online (Sandbox Code Playgroud)

更新#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 …
Run Code Online (Sandbox Code Playgroud)

apache mod-rewrite

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

apache ×1

mod-rewrite ×1