Simulating a 2-level If-Else using RewriteCond
Posted
by hlissner
on Stack Overflow
See other posts from Stack Overflow
or by hlissner
Published on 2010-05-14T09:17:27Z
Indexed on
2010/05/14
12:54 UTC
Read the original article
Hit count: 177
apache
|mod-rewrite
Hi! I'm trying to get my head around RewriteCond, and want to rewrite any requests either to a static html page (if it exists), or to a specific index.php (so long as the requested file doesn't exist).
To illustrate the logic:
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
I don't seem to have much trouble doing it when I don't need to test for the HTTP_HOST. Ultimately, this one .htaccess file will be handling requests for several domains.
I know I could get around this with vhosts, but I'd like to figure out how to do it this way.
Here's where I am at now:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond /default/static/%{REQUEST_URI}.html -f
RewriteRule . /default/static/%{REQUEST_URI}.html [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /default/index.php [L,QSA]
I'm not too familiar with some of the other flags, will any of them be of use here (like chain|C, next|N or skip|S)?
Thanks in advance!
UPDATE: I've managed to do it, but would appreciate alternatives:
RewriteEngine On
RewriteRule ^(.+)/$ /$1 [L]
RewriteCond %{HTTP_HOST} ^(domainA|domainB)\.com [NC]
RewriteCond %{DOCUMENT_ROOT}/%1/static/%{REQUEST_URI}.html -f
RewriteRule (.*)? /%1/static/$1.html [NC,L]
RewriteCond %{HTTP_HOST} ^(domainA|domainB)\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /%1/index.php [L,QSA]
© Stack Overflow or respective owner