Desired result:
http://example.com/ -> index.php
http://www.example.com/ -> index.php
http://hello.example.com/ -
Use the first rule to add the subdomain
parameter, without changing the URI, then use the 2nd rule to route the URI to index.php
:
RewriteEngine On
# Parse the subdomain as a variable we can access in PHP, and
# run the main index.php script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /$1?subdomain=%1
# Map all requests to the 'path' get variable in index.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA]
The second rule needs to have the QSA
flag, otherwise the first rule's query string gets lost.