1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

.htaccess 301 redirect for subdomains

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

  1. GotWeb?

    GotWeb? Banned

    Joined:
    Jun 8, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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:
    subdomain.example.com

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

    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
    /home/user/subdom/public_html/.htaccess
    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:
    subdomain.example.com/sub1/
    subdomain.example.com/sub2/

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

    into the .htaccess file located at
    /home/user/subdom/public_html/.htaccess

    it creates an infinite loop

    If I place .htaccess 301 redirect for JUST
    subdomain.example.com
    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:
    /home/user/subdom/public_html/sub1/.htaccess
    /home/user/subdom/public_html/sub2/.htaccess

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

    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!

    Thanks!

    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

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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

    Joined:
    Jun 8, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Sure, I'm using this code:
    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

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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
    Code:
    RewriteCond %{HTTP_HOST} www.subdomain.example.com$ [NC]
    RewriteRule (.*) http://subdomain.example.com/ [R=301,L]
     
  5. GotWeb?

    GotWeb? Banned

    Joined:
    Jun 8, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    OK, tried it and it's the same problem.

    Putting the codes exactly as you have in the .htaccess file located at:
    /home/user/subdom/public_html/.htaccess
    redirects www to non-www for
    subdomain.example.com

    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:
    /home/user/subdom/public_html/sub1/.htaccess
    does make the redirect from www to non-www work for
    sub1.subdomain.example.com

    BUT breaks the redirect for www to non-www of
    subdomain.example.com
    which uses the .htaccess file at:
    /home/user/subdom/public_html/.htaccess

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

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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:
    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:
    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

    Joined:
    Oct 12, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    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.
    Alex
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    From non www to www is working on Go4Expert atleast. :)
     
  9. internetmarketing

    internetmarketing New Member

    Joined:
    Nov 29, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    SEO Executive
    Home Page:
    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.
    :confused:
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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

    Joined:
    Jan 9, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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!

    regards,

    the G4E newbie
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    sinasylum, try creating a seperate thread for your queries rather than jumping into months old thread.
     
  13. sinasylum

    sinasylum New Member

    Joined:
    Jan 9, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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

    Joined:
    Sep 20, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    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