Forum Moderators: phranque
If the user visits my domain, I want to force them to use 'www.domain.com', and not simply 'domain.com'.
Simultaneously, if the user visits a subdomain of my domain, I want to force them to use 'subdomain.domain.com' and not 'www.subdomain.domain.com'
Basically, if there is no subdomain, force www. If there is subdomain, take away www.
Simple, right? Anyone got any pointers?
check this thread out [webmasterworld.com...]
and BTW, Welcome to WebmasterWorld (its always a pleasure for a newbie to welcome another newbie :))
Now all I need is the subdomain redirections, as stated in my first post.
Post your best-effort code, and we'll help you get it working.
Jim
Like I say, I have no idea what I'm doing, but I'm giving my logic a shot. If I'm on the right track, I'll try to write something up. If not, perhaps you could suggest a method in a similar descriptive way as above, and I'll give it my best shot. Thanks.
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]
www.domain.com works
domain.com works
sub.domain.com works
www.sub.domain.com gives me a 404
I actually spent the afternoon re-learning regex, and that part of your code makes sense to me. I'm slowly grasping the htaccess terms as well, so by all means, your code should work in my mind.
I'm still working on understanding backreferences, so I can't really debug that part of your code yet, but I'm thinking something there is causing an error?
I'm currently using some version of cpanel, and it seems to have a weird way of setting up subdomains (one I'm not too particularly fond of). It basically maps sub.domain.com => domain.com/sub, creating a subdirectory off of public_html. So I did some altering to your code and this is what I found:
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.net
RewriteRule (.*) http://example.net/$1 [R=301,L] When I go to a subdomain like www.sub.domain.com, the above code redirects the page to example.net/sub/extrastuffhere. That is why the original script is producing a 404: sub.domain.com/sub doesn't exist.
So here's yet another topic I'm not too experienced in. Is there a way to tell apache to use the standard /subdomains outside of the /public_html so it doesn't automatically create domain.com/sub with every new subdomain? Or is there a way to alter the code so the variable $1 doesn't contain the subdomain in it during its redirection? I'm leaning towards the latter solution if possible.
[edited by: jdMorgan at 2:03 am (utc) on Oct. 2, 2005]
[edit reason] Example.net [/edit]
Your choices are either to by-pass the control panel and do the (sub)domain-to-directory mapping yourself, or to set up subdirectories for each subdomain that you want to use. This latter option can get unworkable fast if you have many subdomains -or worse- if you want to create them dynamically.
Look into you control panel documentation, and see if it is possible to map *all* (sub)domains to one single directory. If that is possible, you'll be able to use the code posted in several threads [google.com] here to 'sort them out' once you get them all pointed to an .htaccess file in the main domain's root directory.
Jim