Rewrite rule with internal redirect not working properly
<p>I believe the solution I am looking for is quite simple but I have spent almost a day searching for it without success, so I surrender. </p>

<p>I would like to redirect all requests from <code>portail.domain.com</code> to the <code>portail-index.php</code> file, and all other requests (from my other subdomains) to the <code>index.php</code> file. </p>

<p>This is my <code>.htaccess</code> file : </p>

<pre><code># ROUTING portail SUBDOMAIN TO portail-index.php
RewriteCond %{HTTP_HOST} ^portail.*
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^portail-index.php$
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /portail-index.php [NC,L,QSA]


# REROUTING EVERYTHING ELSE TO index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /index.php [NC,L,QSA]
</code></pre>

<p>And here are the log results when I try to access <code>portail.domain.com</code> : </p>

<pre><code> applying pattern '.*' to uri ''
 RewriteCond: input='portail.domain.com' pattern='^portail.*' =&gt; matched
 RewriteCond: input='/home/ubuntu/app/preprod/' pattern='!-f' =&gt; matched
 RewriteCond: input='/home/ubuntu/app/preprod/' pattern='!-d' =&gt; not-matched
 RewriteCond: input='/' pattern='^/$' =&gt; matched
 rewrite '' -&gt; '/portail-index.php'

 trying to replace prefix /home/ubuntu/domain/app/preprod/ with /
 internal redirect with /portail-index.php [INTERNAL REDIRECT]

 strip per-dir prefix: /home/ubuntu/domain/app/preprod/index.php -&gt; index.php
 applying pattern '.*' to uri 'index.php'

 RewriteCond: input='portail.domain.com' pattern='^portail.*' =&gt; matched
 RewriteCond: input='/home/ubuntu/domain/app/preprod/index.php' pattern='!-f' =&gt; not-matched
 RewriteCond: input='/index.php' pattern='^portail-index.php$' =&gt; not-matched

 strip per-dir prefix: /home/ubuntu/domain/app/preprod/index.php -&gt; index.php
 applying pattern '.*' to uri 'index.php'
 RewriteCond: input='/home/ubuntu/domain/app/preprod/index.php' pattern='!-f' =&gt; not-matched
 pass through /home/ubuntu/domain/app/preprod/index.php
</code></pre>

<p>Now I perfectly understand that causing an <code>internal redirect</code> replays the battery of rewrite rules. <strong>What I don't understand is why the second pass doesn't match the <code>portail.domain.com</code> rule again.</strong> </p>

<p>I tried replacing the <code>L</code> flags by <code>END</code> flags, but to no avail. The result was exactly the same. I also tried several adaptations of those rewrite rules, but none of those attempts worked. I spent hours reading the Apache rewrite_mod documentation, so I believe I understand the module quite well, but can't get it to work properly in this case.</p>

<p>Any suggestion as to why it is not working, or even better : how to make this work, is most welcome.</p>

<p><strong>--- EDIT ---</strong></p>

<p>Many thanks for your help @Anubhava. Here are the logs I get after implementing you version of the <code>.htaccess</code> file : </p>

<pre><code>strip per-dir prefix: /home/ubuntu/domain/app/preprod/ -&gt; 
applying pattern '^' to uri ''
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-f' =&gt; not-matched
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-d' =&gt; matched
pass through /home/ubuntu/domain/app/preprod/
strip per-dir prefix: /home/ubuntu/domain/app/preprod/index.php -&gt; index.php
applying pattern '^' to uri 'index.php'
RewriteCond: input='/home/ubuntu/domain/app/preprod/index.php' pattern='-f' =&gt; matched
pass through /home/ubuntu/domain/app/preprod/index.php
</code></pre>

<p>Does that leads you anywhere ?</p>

<p><strong>--- SECOND EDIT ---</strong></p>

<pre><code>strip per-dir prefix: /home/ubuntu/domain/app/preprod/ -&gt; 
applying pattern '^' to uri ''
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-f' =&gt; not-matched
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-d' =&gt; matched
pass through /home/ubuntu/domain/app/preprod/
strip per-dir prefix: /home/ubuntu/domain/app/preprod/index.php -&gt; index.php
applying pattern '^' to uri 'index.php'
RewriteCond: input='/home/ubuntu/domain/app/preprod/index.php' pattern='-f' =&gt; matched
pass through /home/ubuntu/domain/app/preprod/index.php
</code></pre>

<p>I really do not get why calling <code>portail.domain.com</code> doesn't lead to <code>portail-index.php</code>. </p>