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

Redirect subdomain from http://www to just http://

Discussion in 'Website Design Forum:' started by pablo631, Mar 13, 2009.

  1. pablo631

    pablo631 Junior Member

    Is it possible to have a redirect using htaccess that rewrites the a url that uses http://www.sub.domain.com to point to http://sub.domain.com? Sometimes users forget to NOT type in www. when trying to access a subdomain and they end up at my default site on my server. Which is a bit confusing for them. Any ideas?
     
  2. Jazajay

    Jazajay Active Member

    Is the sub domain on the same server as your main domain?
    If it is let me know and we'll try something else.
    But this should do it ~

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^sub\.domain\.com$ [NC]
    RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]


    Any problems let me know as that should work.

    Jaz

    Key:
    Red ~ Mod_Rewrite
     
  3. pablo631

    pablo631 Junior Member

    Thanks for the quick reply. Yeah they are on the same server. The problem is I move the default domain for testing sometimes. So the site that shows up if you just type in the IP (or mistype a subdomain) may be a random website I'm working on, not even related to the the main domain.
     
  4. Jazajay

    Jazajay Active Member

    Well try that first as it should be fine thinking about it.

    If not we can try to get fancy, haven't worked with redirects and sub domains before TBH but there are a few things we can try if it doesn't, but that should work, let me know either way. :)

    Jaz
     
  5. pablo631

    pablo631 Junior Member

    I really appreciate the help, But that was a no go.
     
  6. Jazajay

    Jazajay Active Member

    OK try adding the ServerAlias directive above turning the RewriteEngine on and see if that makes any difference.
    So....

    ServerAlias *.yourdomain.com
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^sub\.domain\.com$ [NC]
    RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]


    Jaz

    Red ~ Mod_Rewrite
     
  7. pablo631

    pablo631 Junior Member

    I'm getting a internal error when I go to the site. Is this a situation where I would just need to modify the htaccess file for the default site for the subdomain. That way they just get redirected to the correct subdomain from the default domain?
     
  8. Jazajay

    Jazajay Active Member

    Are these rules going in the .htaccess file in your root directory?
     
  9. pablo631

    pablo631 Junior Member

  10. Jazajay

    Jazajay Active Member

    I would put them in the main root that being ~ domain.com/.htaccess as the sub domain should still be taking the rules from the main domain. But try the first solution first when you do.
     
  11. Kevin

    Kevin Senior Member

    I never enter www. to reach a subdomain. That's just silly and I've never run into any problems before either :confused:
     

Share This Page