Forum Moderators: phranque
http://a.domain.com ==> http://domain.com/mine.php?a
http://a.b.domain.com ==> http://domain.com/mine.php?ab
http://a.b.c.domain.com ==> http://domain.com/mine.php?abc
I would appreciate any thoughts or pointers on how to accomplish this? Thanks!
I don't normally 'just write the code', but this question isn't posted every day, and I'm not sure if my code will work, and I don't feel like testing it, so you're still kind of on your own, but I would probably start with:
# Store any 1 or more characters, not a dot, in %1, followed by a dot
# if the pattern is present 1 or more times preceding example
# followed by .com in the HTTP_HOST variable.
RewriteCond %{HTTP_HOST} ^(([^.]+)\.)+example\.com$
#Check all requests with 0 or 1 character.
RewriteRule .? http://example.com?%1 [R=301,L]
(…and then start adjusting things when it doesn't work.)
Justin
(.*)is meant to include everything that occurs before the domain name:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule .? http://example.com/url.php?u=%1 [R=301,L]
Both this, and your example, work with subdomains of one word, e.g.,
www.example.comworks. But the moment I include a subdomain with one dot, e.g.,
www.my.example.comthis gives the 500 server error.
Any thoughts or pointers would be great! So many thanks!
[EDIT]
Actually, I tested, and it only works with "www", but not with any other word before the domain, with or without the dots. What am I doing wrong? The Apache docs are a bit frustrating, and presume that I am an expert at regexp, which I unfortunately am not. If you cna point me to some regexp resource for
RewriteCondI will happily try to help myself. Thanks!
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule .? http://example.com?%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.example\.com$
RewriteRule .? http://example.com?%1%2 [R=301,L]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)\.example\.com$
RewriteRule .? http://example.com?%1%2%3 [R=301,L]
Justin
If that doesn't work, I'm not sure, because the pattern you were using should have stored the correct information. Maybe someone else has a better guess as to where the error is coming from... My guess is it is the rule interacting in an unexpected way with another rule in the file, causing a loop.
[edited by: jd01 at 1:47 pm (utc) on May 1, 2007]
But it's not making much sense to me. But I'm trying.
Try adding to the rule/rules:
# Make sure the HOST header is set, and the HOST is not example.com.
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com$
# Above should be added to all rules for this set if more than 1
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)\.example\.com$
RewriteRule .? http://example.com?%1%2%3 [R=301,L]
Justin
Make sure you add the space back between the } and the ! if you copied and pasted before I posted this correction.
Here is my entire .htaccess:
Options +FollowSymlinks -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^my\.only\.subdomain\.example\.com$
RewriteRule .? http://example.com/url.php?u=my.only.subdomain [R=301,L]
Any ideas why even this pretty straightfwd hardcoded example won't work?
How do you test?
- With what URL?
What is the result?
- Do you get a browser error message?
-- If so, what is it?
--- If it's "Internet Explorer cannot display the webpage" or "Server not found," do you have your DNS set up for wild-card subdomains?
- Do you get a server error?
-- If a server error, what is in the server error log?
How does this result differ from what you expect?
Jim
jdMorgan: He's getting a 500 error when attempting to redirect with more than one sub. in the URL, but www. works correctly.
ADDED: I think you got it right here...
do you have your DNS set up for wild-card subdomains?
Glad you were awake.
I missed it.
Justin
The question that applies in this specific case with a server error is: "What is in the server error log?"
If there was a DNS problem, then the request would never arrive at the server, so you wouldn't get a 500 error. I suppose it's still possible that the server itself is not set up to recognize the subdomains, but it's more likely some other problem.
Nothing here rules out a problem with the script itself, either. We need to see that error log info...
Jim
How you set up the DNS zone file --or whether it's even possible-- depends on who owns your domain servers, and what kind of control panel they provide for doing this.
How you set up the server to recognize wild-card subdomains --or whether it's permitted-- depends on your hosting company and what level of access they grant you to the server configuration. The higher-grade hosting plan you have, the more likely that you'll be able to do this. In fact, some hosts configure the server to recognize all subdomains by default, especially if you have a static IP address.
Sorry for all the complications, but there is no simple answer...
Jim
My server is unmanaged however, so I did a google and found that the wildcard subdomain thing is as simple as putting
ServerAlias *.domain.tld
This I did, then restarted Apache, and tested. The main domains (with and without "www") work. Now what can I do?
Depending on your DNS control panel, it might look something like:
*.example.com. 86400 IN A 192.168.0.1
Jim
Still [random.mydomain.com...] does not work. The [mydomain.com...] works. Only "www" works. The .htaccess has no instructions for subdomains anymore.