I want to use .htaccess to rewrite from "virtual" subdomains. Example 1 www.soccer.domain.com to www.domain.com/index.php?k=soccer Example 2 www.cancer-patients.domain.com to www.domain.com/index.php?k=cancer-patients However, I want the URL still displayed as the subdomain, yet it calls upon the index.php file. The tricky thing is though, I want it to do so without the subdomains actually existing. So people could enter random subdomains, (like: www.dfhjkldhfdjlkf.domain.com which would rewrite to www.domain.com/index.php?k=dfhjkldhfdjlkf). Is this possible? Thanks.
Yes you can do that. First enable wild card subdomains for your domain then enter the following into your htaccess file. Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.domain.com RewriteCond %{HTTP_HOST} ([^.]+)\.domain.com RewriteRule ^(.*)$ index.php?k=%1 Code (markup):
You can use the RewriteCond directive in order to check whether current host is a subdomain. RewriteCond %{REMOTE_HOST} ^([^\.]*)\.domain.com$ RewriteRule ^/(.*)$ http://domain.com?k=%1 [L] Code (markup): I don't tested it yet but it should work. Here's the reference for Apache 2 http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html I have to say that I don't really like this apporach. I would prefer to check the domain runtime in the code.
I had tested my code. It will extract the subdomain name from www.subdomain.domain.com and subdomain.domain.com, and will rewrite the url to index.php?k=subdomain, unless the requested page is www.domain.com or domain.com.
Thanks for that both of you. Aragorn, as you answered first I'll give you the $5 Please PM Paypal. Also, how do you set up wildcard domains?
To setup a wild cards DNS record, create a A(address) record for *.domain.com that points to your server's ip address. Most probably your control panel will be having an option for this. If not, you will have to add the line *.domain.com. 14400 IN A {your.web.server.ipaddress} Code (markup): to the end of your ZONE DB file. Don't forget the period after domain name. And then you have to edit your virtual host conf file and add the line ServerAlias *.domain.com Code (markup): You will be able to manually edit your virtual host settings and ZONE DB file, only if you are having a dedicated or VPS hosting. Or else you will have to contact the web hosting company to do it for you. The best option is to ask your web hosting company to set up the wildcard DNS for you. If you want to do it yourself, check http://allyourtech.com/content/articles/25_11_2005_setting_up_wildcard_subdomains_on_apache.php and http://photomatt.net/2003/10/10/wildcard-dns-and-sub-domains/