1

次のような状況でReWrite条件をネストしたいのですが、適切なドキュメントが表示されないという状況に遭遇しました。

RewriteCond %{REQUEST_URI} ^(robots.txt|favicon|ico)$ [NC]
RewriteRule . - [S=3]
# Nested ReWrite Condition 
RewriteCond %{HTTP_HOST} ^www
    RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI_1} [R=301,L]
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI_2} [R=301,L] # and so on

したがって、スキップルールの数がネストされたリライト条件で構成されるかどうか、つまり、この場合、スキップされるリライトルールの数が4または5(リライト条件を含む場合)であるかどうかという疑問が生じます。

4

2 に答える 2

2
RewriteCond %{REQUEST_URI} ^(robots.txt|favicon|ico)$ [NC]
RewriteCond %{HTTP_HOST} !^www
RewriteRule .* - [S=3]

# the following rules are run only if the first 2 conditions don't match
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI_1} [R=301,L]
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI_2} [R=301,L]

!2番目のcond
ドキュメントの否定に注意してください:

RewriteCondはその直後のRewriteRuleにのみ適用されるため、この手法は便利です。したがって、RewriteCondを複数のRewriteRuleに適用する場合、考えられる手法の1つは、これらの条件を無効にして、[Skip]フラグを指定してRewriteRuleを追加することです。

于 2012-12-25T13:12:36.503 に答える
1

例を投稿しただけなので、その仕組みの例を示します。コメント付きですが、それでも十分に話せない場合は、ここでさらに多くの説明を利用できます

# Does the file exist?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Create an if-then-else construct by skipping 3 lines if we meant to 
# go to the "else" stanza.
RewriteRule .? - [S=3]

# IF the file exists, then:
    RewriteRule (.*\.gif) images.php?$1
    RewriteRule (.*\.html) docs.php?$1
    # Skip past the "else" stanza.
    RewriteRule .? - [S=1]
# ELSE...
    Rewri

これで問題が解決するはずです。そうでない場合は、質問の例を更新して、不足しているものが明確になるようにしてください。

はい、条件ではなくルールをスキップします。

于 2012-12-25T13:38:41.087 に答える