redirect subdomain in .htaccess

Discussion in 'Apache' started by redsy, Feb 14, 2009.

  1. #1
    Hi

    I have been developing a site and the test subdomain got indexed, i forgot to block with robots.txt.

    I would like to redirect the subdomain and all the pages, For both www and non www to www.domain.net
    for eg
    dev.domain.net to domain.net
    www.dev.domain.net/page to www.domain.net
    www.dev.domain.net/page to www.domain.net

    There is no .htaccess at all at present so would like all requests for to be redirected to www.domain.net if possible.

    At the same time if i can do a non www to www redirect that would be great. Any help would be very gratefully received, thanks guys.
     
    redsy, Feb 14, 2009 IP
  2. websecrets

    websecrets Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    .htaccess alone won't be able to do this for you. You will need to setup your own nameservers or at least be able to configure access to your main nameservers to change the requests that come in to it.

    I fixed this by pointing my DNS information to one of my servers which returned a script generated response to wherever the request came from.

    Figuring out how to do this took me a couple days and I even ended up writing a tutorial for myself just incase I had to do it again. I'll see if I can find that tutorial and post it or send it to you.
     
    websecrets, Feb 17, 2009 IP
  3. redsy

    redsy Member

    Messages:
    209
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    33
    #3
    that would be really appreciated, thanks for your time
     
    redsy, Feb 18, 2009 IP
  4. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #4
    Assuming the subdomain is resolving fine and I don't see anywhere where you say you deleted it then the following in the .htaccess of the subdomain should do what you ask:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.domain\.net[NC]
    RewriteRule (.*) http://www.domain.net/$1 [R=301,L]
    Code (markup):
    You can also use it on the main domain to redirect non-www requests to www versions.
     
    tolra, Feb 18, 2009 IP
  5. transitnetwork

    transitnetwork Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    # Extract the subdomain part of domain.com
    RewriteCond %{HTTP_HOST} ^([^\.]+)\. domain\.com$ [NC]

    # Check that the subdomain part is not www and ftp and mail
    RewriteCond %1 !^(www|ftp|mail)$ [NC]

    # Redirect all requests to a php script passing as argument the subdomain
    RewriteRule ^$ http://www.domain.com/%1/details.html [R,L]

    I use this code for wildcard dns, might help.
     
    transitnetwork, Feb 19, 2009 IP
  6. redsy

    redsy Member

    Messages:
    209
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    33
    #6
    thanks guys. As we are continuing to use subdomain, i have blocked for time being.

    Am no apache expert.

    The domain root is forwarded with an apache proxy (my brother tells me, hes no good with htacess)

    ie domain/folder is redirected from domain

    http//domain redirects to www.domain fine

    however domain/folder typed direct into address bar remains as this and does not redirect to www.

    I am using a std non www to www htaccess at present.
     
    redsy, Feb 22, 2009 IP
  7. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #7
    If you use the .htaccess I posted above on the main domain then it should be redirecting domain/page to www.domain/page.

    Or post your .htaccess so it can be debugged.
     
    tolra, Feb 22, 2009 IP
  8. redsy

    redsy Member

    Messages:
    209
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    33
    #8
    Hi

    Htaccess as is with the actual domain having probs with. Site is written in java.

    RewriteEngine On

    RewriteCond %{HTTP_HOST} !^www\.cpdregister\.net [NC]
    RewriteRule (.*) http://www.cpdregister.net/$1 [R=301,L]

    this should redirect from http://cpdregister.net/cpd/ to http://www.cpdregister.net/cpd/ but doesnt

    thanks for help.
     
    redsy, Feb 23, 2009 IP
  9. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #9
    tolra, Feb 23, 2009 IP