Is it possible to use if-else statements?

New to mod_rewrite? This is a good place to start.

Is it possible to use if-else statements?

Postby hijack » Tue Sep 15, 2009 7:56 am

Is there something similar to the if-else statements that exist on other languages like PHP?

I'd like to apply one rule if the host is domain.com and another if the host is any other

Fo example:

Code: Select all
if HOST is domain.com

   ...

else

   ...


Thanks.
hijack
 
Posts: 5
Joined: Tue Feb 26, 2008 4:18 am

Postby richardk » Tue Sep 15, 2009 6:02 pm

No. You can only do IF something. However, you can still do it
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Just for example.com.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^abc$ /def [QSA,L]

# Everything else.
RewriteRule ^abc$ /ghi [QSA,L]

because the other domains won't match the first rule but will match the second. And example.com will match the first rule and stop (the L flag).

Sometimes you may need
Code: Select all
# (!) Not example.com.
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]

for "everything else".
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 24 guests

cron