Forum Moderators: phranque
Hosted on xyz host (the name doesn't matter, it's just standard virtual hosting on a CPanel-managed site).
DNS for the domain (which has wildcard set) is managed by afraid, delegation is with their ns[1-4].
The VirtualHost wrapper on the server is configured with both www.dom.tld and *.dom.tld (as the ServerAlias), and all *.dom.tld requests are passed correctly to the server.
However, my idea is that I have .htaccess defining how to listen to the subdomains being passed to the server for request, and handling them differently... If the subdomain is virtual, the eventual goal is to dynamically-source content from an SQL database, and if the subdomain physically exists (i.e. it's mapped to an automatically-created subdomain in the root, as CPanel does it), the browser is transparently passed to the real subdomain's contents.
I made a sample script to just display whether the subdomain existed or not, and display the passed-through variables (passed from .htaccess via rewriterule to the php script directly and subsequently displayed via the PHP script).
I'm not having much luck actually making the server point me in the right place though... I've disabled the php script to try and see what the hell's going on here... all subdomains just point to the root, regardless of whatever RewriteRule I've set for one of the defined subdomains (I gave up trying to redirect dynamically, I've defined them statically for the moment to try and make it work!). I can use any subdomain to access the root, even if I define the required (matching) subfolder - effectively that subdomain's root folder according to CPanel - which is wrong, the subdomains should redirect to the subfolder... Well, it's how I'd like it to work. Any other (wildcard) subdomain requests will, eventually, be passed to the PHP script.
I can't start building the main site if this relatively simple exception to the rule refuses to co-operate with me!
Here's my htaccess (prepare yourself):
RewriteEngine On
Options +FollowSymlinks
# ###################################### #
# ###################################### #
# ok, couldn't be bothered. gave up with #
# dynamically-detecting existing dirs, #
# just define them here for the moment. #
# ###################################### #
RewriteCond %{HTTP_HOST} ^leeching\.example\.co.uk [NC]
RewriteRule ^/(.*)$ /home2/christof/public_html/leeching/$1 [L]
RewriteCond %{HTTP_HOST} ^mixset\.leeching\.example\.co.uk [NC]
RewriteRule ^/(.*)$ /home2/christof/public_html/mixset/$1 [L]
RewriteCond %{HTTP_HOST} ^school\.example\.co.uk [NC]
RewriteRule ^/(.*)$ /home2/christof/public_html/school/$1 [L]
RewriteCond %{HTTP_HOST} ^tv\.example\.co.uk [NC]
RewriteRule ^/(.*)$ /home2/christof/public_html/tv/$1 [L]
# ###################################### #
# make www. into http:// for no-www.org
# RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.co.uk$ [NC]
RewriteRule ^(.*)$ [example.co.uk...] [R=301,L]
# RedirectMatch permanent ^/leeching$ [leeching.example.co.uk...]
# CPanel put that in... unwanted (it's redirect! doesn't stay in subdomain)
# this first bit, inspiration from [webmasterworld.com...] (checking for physical dir existence)
# and [webmasterworld.com...]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [L]
#RewriteCond %{HTTP_HOST} ^([^.]+)\.example.co.uk [NC]
#RewriteRule .* /home2/christof/public_html/index.php?sucks=%1 [L]
# 1. If it's just the domain or the www hostname, leave the URL alone.
RewriteCond %{HTTP_HOST} (www\.¦[^(.*)]){1}example.co.uk [NC]
RewriteRule .* - [L]
# 2. If it's any other hostname, serve the file index.html from the hostname directory.
#RewriteCond %{HTTP_HOST} ^([^.]+)\.otherdomain.tld [NC]
#RewriteRule .* /home2/christof/public_html/other.php?dom=%1 [L]
# 3. If it's a hostname within a subdomain (ex: hostname.subdomain.domain.com), use the file hostname.html from the subdomain directory.
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.example.co.uk [NC]
RewriteRule .* /home2/christof/public_html/index.php?sucks=%1+%2 [L]
# original... RewriteRule .* /home2/christof/public_html/%2/%1.html [L]
# 3a. (my mod) As 4, except only looks for one subdomain.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.co.uk [NC]
RewriteRule .* /home2/christof/public_html/index.php?sucks=%1 [L]
Things I'm thinking about, but am unsure about (I'm just going round in circles seemingly):
At the moment CanonicalNames is turned to On, as default. When I had an earlier version of my htaccess going, subdomains worked initially, but as Canonical was turned on, references to going up a folder, etc all point to [example.co.uk...] - which broke the look and feel totally of the site. As I'm trying to run separate content entirely on a couple of these subdomains, obviously it breaks scripts and CMSs because they can't request documents, CSS files, other files properly...
If I delegate authority to the nameservers of my webhost, instead of afraid's NSs, that'll remove the wildcard ability yes, but will it help solve the problem? What about adding the ns1 and ns2 of my webhost (they only have two) as secondaries to afraid's entries?
Is there any way OTHER than modifying httpd.conf to dynamically change the DocumentRoot for a subdomain? (I have no access, and don't wish to have to make an entry for each real (i.e. non-wildcard) subdomain, it's supposed to be dynamic as possible this site.)
Am I missing something here? I'm not a regexpert, I understand the basics, getting there slowly, but like anything new it's learn-as-you-do, I've still found nothing really helpful for this (ever get the feeling you're overstretching your abilities heh).
I came here finally because I'm confident that people 1) are cleverer than me when it comes to matters like these, 2) I haven't a clue, and 3) I feel stupid. ;)
Any ideas, suggestions or tips are most GRATEFULLY appreciated (and received!)
I hope this becomes as much a reference to others attempting to achieve the same end(s) as I in the future as much as a solution to this myriad of problems. :)
Cheers in advance
Christopher.
Welcome to WebmasterWorld!
First quick suggestion: In .htaccess, the requested URI "seen" by RewriteRule is localized to the current directory. All path elements to the current directory are removed. What this means practically, is that in your Web root directory, the URI seen by RewriteRule will not start with a slash.
You will also need to explicitly check for recursion. Your httpd.conf and .htaccess files will be called again after any .htaccess rewrite takes place so that any rules which apply to the newly-rewritten URIs can be applied. This can lead to inifinite rewrite loops if they are not explicitly prevented.
Also, absolutely avoid checking for 'file exists' and/or 'directory exists' in an unqualified manner. Use any other conditions you can find to avoid doing this, and make the rule as specific as possible. Otherwise, you will have one very slow server as soon as you put it under load.
Let's keep your code simple at first, get that working, and then duplicate what works and add complexity later. :)
Jim
Hate to say this, but the vast majority of my htaccess contents have been borrowed or adapted from other examples and working solutions found elsewhere, and I've tried (seemingly in vain thus far) to adapt them and manipulate them to achieve the end I'm after.
Right, let's go have a beer and think about this. Time to long-term loan that regular expression book from my library ;)
Cheers for the pointers thus far... I'll report back soon as I make any changes (and what results occur when I do). If I can figure out what to attack first. :D Perhaps one of the most frustrating things is that as I'm deving for linux and I only have win32 platform at my house, everything's via FTP... which is zzzzz slow. Can't even go and repoint the primary A record because people are already loading things from the site... Argh haha I do enjoy making life difficult for myself. ;)
Will check back soon. :thumbsup:
Changed one of my entries for my manually-defined subdomains around a little bit, it now reads:
RewriteCond %{HTTP_HOST} ^leeching\.example\.co.uk [NC]
RewriteRule .* /home2/christof/public_html/leeching [L]
This initially redirects correctly, showing the subdomain in the address bar and showing the directory index. Success! you think - but then when you look at the links the server generates, they're all in the format [example.co.uk...] - not right...
... Would setting CanonicalNames Off solve this problem (and have the browser pass the HTTP_HOST instead of the server generate it from its listed entry (which is example.co.uk as defined in the virtualhost)?
I've redelegated the DNS back to the DNS server of my webhost (as opposed to running them from the afraid.org servers) - I thought this might help CPanel recognise which subdomain was being queried, but it elicits the same results (I still have the *.domain ServerAlias set in the httpd.conf - I'd have to email my webhost again to get them to remove this, so I thought I'd try everything else first).
Even more bizarrely, and this is something I noticed right from the off, but put down to unusual DNS setup in the first place... If I make a rule such as
RewriteCond %{HTTP_HOST} ^([(tv¦leeching¦school¦mixset)^.]+)\.domain.tld [NC]
RewriteRule ^/(.*)$ /home2/christof/public_html/%1 [L]
you would expect that an input URL of [school.domain.tld...] would make the script redirect to /home2/christof/public_html/school , and then perform all requests from that folder... Right?
Well, not on MY webserver, oh no. For some reason, it's either ignoring the variable, or it's just not processing the rewrite rule properly. I can't figure out why in the world this is happening, but I've had little experience in understanding how CPanel alters the Apache behaviour (I understand that I'm already a folder on the host anyways, but I would've thought that CPanel could handle this rather simple kind of rewrite rule as all the homedirs are virtualised anyway).
I've validated the rules I'm using with various regex validators, hell I've rewritten them from scratch and gone through them making sure I have the right vars... Nothing works properly on my server. The whole idea of this was to mask the fact that you're getting redirected to a folder (but even setting the [R] flag doesn't seem to work!), I've tried the QSA flag too, nowt. zip. zilch. nada.
What's even more frustrating is that I can't implement the rewritelog feature (says "not allowed here", so I guess it's been locked up by my webhost)... I'm about to email them to ask if I can have access to the RewriteLog and RewriteLogLevel features so that I can more effectively bugfix... But this is incredibly annoying.
Can anybody else spot any problems that I've (somehow) overlookd?
Cheers
Christopher.