有没有办法在Apache htaccess中if / else重写?

时间:2012-08-10 16:54:01

标签: apache .htaccess webserver

我想在.htaccess文件中创建以下结构:

If (SITE A DOMAIN or SITE A SUBDOMAIN) {  
   Rewrite this specific way ->  /index.php/$1 
} else {
   Rewrite this specific way ->  /directory/www/index.php/$1
}

我目前的代码如下所示,但是我的Apache错误日志抱怨了这个问题:

[error] [client :: 1] mod_rewrite:达到的最大内部重定向数。假设配置错误。如果需要,使用'RewriteOptions MaxRedirects'来增加限制。,referer:

我做错了什么?谢谢!

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|blog)

RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^ example\.com$
RewriteRule .? - [S=1]
RewriteRule ^(.*)$ /directory/www/index.php/$1 [L]
RewriteRule .? - [S=1]
RewriteRule ^(.*)$ /index.php/$1 [L]

1 个答案:

答案 0 :(得分:17)

使用条件包含/排除跳过标记可能会更清晰一些(可能会让人感到困惑)。但看起来有一个重定向循环导致500.试试这个:

# If (SITE A DOMAIN or SITE A SUBDOMAIN) 
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
# make sure we haven't already rewritten this URI
RewriteCond %{REQUEST_URI} !/directory/www/index.php
# don't rewrite if the resource already exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite
RewriteRule ^(.*)$ /directory/www/index.php/$1 [L]

# IF !(SITE A DOMAIN or SITE A SUBDOMAIN) 
RewriteCond %{HTTP_HOST} !^subdomain\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
# make sure we haven't already rewritten this URI
RewriteCond %{REQUEST_URI} !/index.php
# don't rewrite if the resource already exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite
RewriteRule ^(.*)$ /index.php/$1 [L]