I have the following .htaccess code:
RewriteEngine on
<if "%{HTTP_HOST} =~ /^helpdesk\./">
RewriteRule ^/?$ /index.php?pages=helpdesk&file=index [NC,L,QSA]
</if>
<if "%{HTTP_HOST} =~ /^account\./">
RewriteRule ^/?$ /index.php?pages=account&file=index [NC,L,QSA]
</if>
Obviously, this is not working.
What I want to achieve:
when I go to http://helpdesk.domain.com, the index.php file must get the parameters pages=helpdesk and file=index. When I go to http://account.domain.com the index.php file must get the parameters pages=account and file=index.
When I replace the RewriteRule with a redirect, it works, but is this possible to achieve?
Thanks in advance!
Edit:
asked my question way to fast. This works.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^helpdesk\..* [NC]
RewriteRule ^/?$ /index.php?pages=helpdesk&file=index [NC,L,QSA]
RewriteCond %{HTTP_HOST} ^account\..* [NC]
RewriteRule ^/?$ /index.php?pages=account&file=index [NC,L,QSA]
But is this the correct way or is there a (better) alternative?
Best Answer
What you are trying to achieve is possible with both, and your currently working configuration is the traditional way of doing this. That's why there's plenty of examples for doing this with
RewriteCond
. The Expressions and directivesIf
,ElseIf
,Else
and were introduced in Apache 2.4 as a long-awaited feature, and one of the first examples is to make rewriting more readable and predictable:Without testing, I'd say that using regular expressions with your use case makes this overly complicated. Because you are comparing
Host:
headers probably within a fixed domain, sayexample.com
, you could use simple==
(string equality) comparison operator instead of=~
(string matches the regular expression):