1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

.htaccess 301 redirect for subdomains

Discussion in 'Search Engine Optimization (SEO)' started by GotWeb?, Jun 9, 2006.

  1. GotWeb?

    GotWeb? Banned

    Hi, I hope I'm asking this in the right forum?

    I've been searching for an answer to this problem for 5 days, and hopefully someone here knows the solution.

    Firstly, I'd like to provide the information that I too am a programmer, experienced in Perl, PHP, JS, Java, VRML, and a little C++, Python, and Delphi.

    I have a domain: example.com
    that I've created a full independent subdomain for which does not live under the root folder path of example.com

    This is a fully qualified subdomain with it's own dns and it's own file path from root that does not connect to the main domain example.com at all. I created the subdomain in WHM on my server backend.

    The subdomain is for example:

    Then, under the subdomain.example.com folder path (in CPanel) I created the subs of:

    Now, the root paths for subdomain.example.com
    is: /home/user/subdom/public_html/

    The root path for the subs of subdomain.example.com
    is: /home/user/subdom/public_html/sub1/
    is: /home/user/subdom/public_html/sub2/

    These are browser reachable by both www and non-www versions.
    Google has issues with this big time.

    I've tried redirecting subdomain.example.com
    from www to non-www
    via .htaccess file located at
    and it works fine and does the redirect from www to non-www

    But, it forces the subs to their respective folders Vs being treated as sub.subdomain.example.com

    In other words, the subs go to:

    If I put 301 .htaccess redirects in for
    subdomain.example.com AND
    sub1.subdomain.example.com AND

    into the .htaccess file located at

    it creates an infinite loop

    If I place .htaccess 301 redirect for JUST
    at: /home/user/subdom/public_html/.htaccess
    it redirects www to non-www

    then placing the same for the subs located in their respective folders:

    it redirects the sub.subdomains from www to non-www
    BUT breaks the www to non-www redirect for

    Does anyone have any idea how the heck this is supposed to be done? Quite honestly, I'm going out of my mind trying to find the answer.

    At help would be very greatly appreciated!


    Also, I like this forum and plan to stick around. I'm a good programmer and maybe I can help someone else when they need answers, too? :)
  2. shabbir

    shabbir Administrator Staff Member

    Can you let us have the content of the .htaccess file you are using for redirecting it from www to non-www version.
  3. GotWeb?

    GotWeb? Banned

    Sure, I'm using this code:
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^subdomain.example.com$ [NC]
    RewriteRule (.*) http://subdomain.example.com/$1 [R=301,L]
  4. shabbir

    shabbir Administrator Staff Member

    Just try with hard coded www rather than ^.

    What I meant is
    RewriteCond %{HTTP_HOST} ^subdomain.example.com$ [NC]
    means redirect starting with anything

    try changing to
    RewriteCond %{HTTP_HOST} www.subdomain.example.com$ [NC]
    RewriteRule (.*) http://subdomain.example.com/ [R=301,L]
  5. GotWeb?

    GotWeb? Banned

    OK, tried it and it's the same problem.

    Putting the codes exactly as you have in the .htaccess file located at:
    redirects www to non-www for

    If you put a second condition in that same .htaccess file to handle the www to non-www redirect for the sub.subdomains -- it creates an infinite loop

    Remove the second condition from the root .htaccess file and place it instead inside the .htaccess file for the sub.subdomain at:
    does make the redirect from www to non-www work for

    BUT breaks the redirect for www to non-www of
    which uses the .htaccess file at:

    Any ideas? Is this even possible to do, I wonder?
  6. shabbir

    shabbir Administrator Staff Member

    If I am getting you correctly all you need is redirect everything to the non-www version. Correct me if I am wrong.

    Try this out
    To go from the non 'www' to 'www' use this code:
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    To go from the 'www' to non 'www' use this code:
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]
    RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]
  7. Alex TJ

    Alex TJ New Member

    Hi Gotweb,

    Could you please let us know if that suggestion worked for you ok as I have exactly the same problem with non-www to www for subdomains.

    Thanks, and look forward to seeing you guys around the forum.
  8. shabbir

    shabbir Administrator Staff Member

    From non www to www is working on Go4Expert atleast. :)
  9. internetmarketing

    internetmarketing New Member

    Hi Shabbir...
    Above Code doesn't works for me.

    I want to redirect my url http://forums.b2binternetmarketingagency.com/ to http://forums.b2binternetmarketingagency.com/forum

    I applied this kind of code many times but it show this kind of error.....
    when i type address http://forums.b2binternetmarketingagency.com it shows like below...
    http://forums.b2binternetmarketingagency.com/forum/forum/forum/forum........................ and so on.

    Please help me how can i resolve this problem.
  10. shabbir

    shabbir Administrator Staff Member

    The above should work for www to non www and vice versa but I am not sure on redirecting to a particular folder should work using the above one.
  11. sinasylum

    sinasylum New Member

    I am trying to make my site do this...

    when cpanel creates a sub-domain it just puts the root folder of the sub-domain as a sub-folder of the primary domain on the account. so http://sub.domain.com would also be http://www.domain.com/sub/.

    but I want to make it where two things happen... if someone accesses a sub-domain with www, it rewrites the subdomain url without www, and also when someone accesses my subdomain as a sub-folder of the root domain, it also rewrites that and directs the user to the subdomain url.

    Anyone know how to do both or either? thanks for all feedback!


    the G4E newbie
  12. shabbir

    shabbir Administrator Staff Member

    sinasylum, try creating a seperate thread for your queries rather than jumping into months old thread.
  13. sinasylum

    sinasylum New Member

    sorry sharbbir. i wasn't trying to mess things up, i just saw a similar topic and thought i might be better off doing it here. i will start a new thread for the matter. i actually thought by making a new one i may get the response of, "it has already been addressed in another existing thread". I was wrong :eek:) thanks
  14. shabbir

    shabbir Administrator Staff Member

    I just did not mean that you should not be going through the threads but if this thread does not have what you are looking for you should always be going with the new one.
  15. neteragroup

    neteragroup New Member

    Thanks shabbir, this worked for me and took me a long time scouring the internet to find a workable solution with my dynamic subdomains. Much appreciated!

Share This Page