Apache mod_rewrite for wildcard subdomains
I want to make the following work, without changing the web browser address bar.
Start with:
Should become:
I have gotten the DNS set up to accomplish this, and have gotten the VirtualHost setup to serve correctly. My problem is figuring out the correct code in the .htaccess file. I have also tried using the ENV flag (e.g. [E=subdomain:%1]), but have had no success.
I have been able to get the above URL rewriting to work by forcing it in the browser, but all of my attempts to keep it under the covers of the browser address bar causes infinite rewriting.
Any suggestions?
3 Replies
Something like
RewriteCond %{HTTP_HOST} !^www-staging
RewriteCond %{HTTP_HOST} ^([^.]+)\.www-staging\.domain\.tld [NC]
RewriteRule ^(.*)$ http://www-staging.domain.tld/$1?subdomain=%1 [NS,QSA,E=subdomain:%1]
Have you tried doing something like
RewriteCond %{QUERY_STRING} !subdomain=
?
Edit: Reading your second post, it seems like you are doing a HTTP redirect (the external URL in your RewriteRule). Is this what you want to do?