欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

RewriteCond

The RewriteCond directive defines conditions under which the rewriting should take place.

 

Syntax

We should really encourage people to use the lexicographically equal operator instead of a RegEx if they want to ckeck, if test string is lexicographically equal to cond pattern.

E.g. using

 

RewriteCond %{HTTP_HOST} !=""

instead of

 

RewriteCond %{HTTP_HOST} .
# or
RewriteCond %{HTTP_HOST} !^$

or using RewriteCond {REQUEST_URI} !=/foo/bar  instead of

 

RewriteCond %{REQUEST_URI} !^/foo/bar$

or RewriteCond {SERVER_PORT} =443  instead of

 

RewriteCond %{SERVER_PORT} ^443$

Note: Conditions are being processed after the pattern of the RewriteRule has matched. This means that the Condition in following example would be useless (it's always true):

 

RewriteCond %{REQUEST_URI} \.html$
RewriteRule \.html$ - [G]

While this one wastes performance:

 

RewriteCond %{REQUEST_URI} ^/([^.]*)\.html$
RewriteRule ^/(.*) /%1.php [PT]

Every Request matches the rule-pattern and after that the condition will be checked. But you can easily check the uri value in the rule-pattern, so that there is no need for such a condition here:

 

RewriteRule ^/([^.]*)\.html$ /$1.php [PT]

Examples

See ConditionalRewrites

来自 https://wiki.apache.org/httpd/RewriteCond
 

普通分类: