Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to if/else a rewrite in Apache htaccess?

I want to create the following structure in my .htaccess file:

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
}

I have my current code below, but it's throwing 500s with my Apache error logs whining about:

[error] [client ::1] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary., referer:

What am I doing wrong? Thanks!

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]
like image 952
Tim Jahn Avatar asked Aug 10 '12 16:08

Tim Jahn


1 Answers

It may be a little cleaner to just include/exclude using conditions than the skip flags (which can get kind of confusing to read). But it looks like there's a redirect loop causing the 500. Try this:

# 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]
like image 118
Jon Lin Avatar answered Nov 26 '22 12:11

Jon Lin