Matti Matti - 29 days ago 8
Apache Configuration Question

.htaccess subdomains to _GET

I'm trying to use .htaccess rewriting to redirect subdomains to PHP variables.

I'd like it to work like this:

User enters:

subdomain.domain.com/randompage

Page redirects to:

domain.com/index.php?sub=subdomain&page=randompage

But the user doesn't see the URL change.

Kind of like how this works but with the subdomains:

RewriteEngine on
RewriteRule ^(.*)$ index.php?page=$1 [NC,QSA]


This is my first time using stackoverflow, thanks for the help in advance!

Answer

You can use:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$  [NC]
RewriteRule ^(.*)$ index.php?sub=%1&page=$1 [QSA,L]