Forum Moderators: phranque
[sub.example.com...]
into
http://example.com/query.php?q=sub
using mod rewrite? is this possible?
Please see the references cited in our Forum Charter for more information.
Jim
If after reading our Forum Charter [webmasterworld.com], you have a specific question that comports with our purpose here, feel free to ask.
Jim
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)\.example\.com /query.php?q=$1 [L]
to turn [sub.example.com...]
into
http://example.com/query.php?q=sub
This brings up another question, though. What do you intend to do if a "page" is specified in the URL? Do you want to skip the rule, move the page-path into a second php variable, or do something else with it?
Putting that off for the moment, the following change will make your rule work, but only for the home page of any given subdomain:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ /query.php?q[b]=%1[/b] [L]
You may also want to exclude the "www" subdomain from being rewritten to your script -- That's up to you to decide.
Jim
This works, now, but only for www.example.com which sends me to http://example.com/query.php?q=www - but does not for say foo.example.com.
I was hoping to add a rule (below) so that it does not enact the rule for specific pages OR for 'www'
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ /query.php?q=%1 [L]
You didn't say specifically what the results were when you tested "foo.example.com", and "it doesn't work" is not useful for diagnosing a problem, but since it's possible that you received a domain-resolution-related error, the following points should be investigated:
1) Do you have an "A record" in your DNS zone file set up to point wildcard subdomains to your server's IP address?
2) Does your server have a unique (non-shared) IP address? (If not you'll need to add each subdomain using whatever "control panel" your host provided, and you may not be able to use the code method we're discussing here.)
Jim
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ /query.php?q=%1 [L]
Placed onto a unique IP with a hosting who was willing to add a wildcard subdomain.
[edited by: jdMorgan at 5:31 pm (utc) on June 3, 2008]
[edit reason] Snipped hosting company name. See TOS. [/edit]