htaccess, 301 redirect and subdomains -> question

Discussion in 'Apache' started by alexo, May 20, 2006.

  1. #1
    Hello

    2 years ago i set 301 redirect(htaccess) from non www version to www version

    something like this
    
    
    RewriteCond %{HTTP_HOST}!^www\.domain\.com
    RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
    Code (markup):

    today i need to set up subdomain on this domain, and check that my subdomain is redirected to www.domain.com/subdomain/

    hmm ..

    is there any way, (may be some additional code to add), and get subdomain.domain.com instead of domain.com/subdomain/

    thanks
     
    alexo, May 20, 2006 IP
  2. alexo

    alexo Well-Known Member

    Messages:
    372
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #2
    any advise will be appricated

    is it possible to set subdomain, if u have such code in htaccess file?
    if no, what to do?

    thanks
     
    alexo, May 22, 2006 IP
  3. Jayesh Lodha

    Jayesh Lodha Peon

    Messages:
    8
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This problem I am facing is highly technical in nature. I do not have any clue. I read many forums online but did not came to conclusion.

    Even though my posting is too long which is because I have mentioned each and every facts about my problem so that It will be easy to make you understand what problem I am facing.

    Kindly Read it very carefully.

    Kindly help me.

    I have following subdomains , domains and files.

    http://www.mydomain.com/index.htm
    http://www.mydomain.com/1.htm
    http://www.mydomain.com/2.htm

    http://subdomain1.mydomain.com/index.htm
    http://subdomain1.mydomain.com/1.htm
    http://subdomain1.mydomain.com/2.htm

    http://subdomain2.mydomain.com/index.htm
    http://subdomain2.mydomain.com/1.htm
    http://subdomain2.mydomain.com/2.htm

    -----------

    I want to set following 301 permanent redirection using .htaccess :
    ( non www to www )

    http://mydomain.com/
    must redirects to
    http://www.mydomain.com/

    http://mydomain.com/index.htm
    must redirects to
    http://www.mydomain.com/

    http://mydomain.com/allfile.htm
    must redirects to
    http://www.mydomain.com/allfile.htm

    http://mydomain.com/allfolder/allfile.htm
    must redirects to
    http://www.mydomain.com/allfolder/allfile.htm

    -----------

    I also want to set up following 301 permanent redirection using .htaccess :
    ( www to non www for subdomains )

    http://www.subdomain1.mydomain.com/index.htm
    must redirect to
    http://subdomain1.mydomain.com/

    http://www.subdomain1.mydomain.com/allfile.htm
    must redirect to
    http://subdomain1.mydomain.com/allfile.htm

    and

    http://www.subdomain1.mydomain.com/allfolder/allfile.htm
    must redirect to
    http://subdomain1.mydomain.com/allfolder/allfile.htm

    ------------

    I also want to set up following 301 permanent redirection using .htaccess :
    ( www to non www for subdomains )

    http://www.subdomain2.mydomain.com/index.htm
    must redirect to
    http://subdomain2.mydomain.com/

    http://www.subdomain2.mydomain.com/allfile.htm
    must redirect to
    http://subdomain2.mydomain.com/allfile.htm

    and

    http://www.subdomain2.mydomain.com/allfolder/allfile.htm
    must redirect to
    http://subdomain2.mydomain.com/allfolder/allfile.htm

    -------------

    I HAVE GOT FOLLWING DIFFERENT SOURCE CODES AT DIFFERENT FORUMS ONLINE BUT I DO NOT KNOW WHICH ONE IS CORRECT AND WHICH ONE IS NOT SINCE I DO NOT HAVE WORKED ON .HTACCESS BEFORE IN MY LIFETIME. NEITHER I KNOW APACHE AND LINUX.

    So kindly provide me Source Code which is suitable for my requirement. Also let me know whether it is safe for search engines or not ?

    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]

    =================================================================

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^digitallywise\.com
    RewriteRule ^(.*)$ http://www.digitallywise.com/$1 [R=permanent,L]

    =================================================================

    RewriteEngine On

    RewriteCond %{HTTP_HOST} !^www.hwtskins.co.uk$
    RewriteRule ^(.*)$ http://www.hwtskins.co.uk/$1

    =================================================================

    So let me know the exact source code which I can use for this purpose.

    It will be great help.
     
    Jayesh Lodha, Nov 3, 2006 IP
  4. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #4
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^DOMAIN\.com [NC]
    RewriteRule ^(.*)$ http://www.DOMAIN.com/$1 [R=301,L]

    for making www. get added.

    RewriteRule ^index.htm$ http://www.domain.com/ [R=301,L]

    to get rid of index.htm unless is messes up domain.com with out index.htm
     
    Nintendo, Nov 3, 2006 IP
  5. Jayesh Lodha

    Jayesh Lodha Peon

    Messages:
    8
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for your reply.

    So you mean to say I use http://www.mydomain.com/ every where instead of http://www.mydomain.com/index.htm

    Is there any difference for search engine ranking and page rank purpose, If I use any of these following links :

    http://www.mydomain.com
    and
    http://www.mydomain.com/

    ---------------------------

    You have provide the code which change non www url to www url as below :

    http://mydomain.com will be redirect to http://www.mydomain.com

    Once these code will be in place, Will sub domains will also be redirect to www version of subdomain as well ?

    i.e.

    http://subdomain1.mydomain.com/ -> http://www.subdomain1.mydomain.com/

    http://subdomain2.mydomain.com/ -> http://www.subdomain2.mydomain.com/

    If so, how to redirect these www versions of subdomain ( i.e. http://www.subdomain1.mydomain.com ) to non www version of subdomain ( i.e. http://subdomain1.mydomain.com ) ?

    I think whether it is redirected to non www version of subdomain to www version of subdomain or not, I must redirect www version to non www version of subdomain so that there will not be any issue at all. Do you think, I am right here ? If not let me know since I very much care search engine and want to know whether search engine will treat as spam or it will treat just a 301 redirection.

    Thanks Again
     
    Jayesh Lodha, Nov 3, 2006 IP
  6. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #6
    This is a guess for geting www out of sub-domains......

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.subdomain1\.DOMAIN\.com [NC]
    RewriteRule ^(.*)$ http://subdomain1.DOMAIN.com/$1 [R=301,L]

    You only have to worry about index.htm in URLs if you link to URLs with them there.
     
    Nintendo, Nov 3, 2006 IP
  7. Jayesh Lodha

    Jayesh Lodha Peon

    Messages:
    8
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So according to you, code to do following task is as below :

    For non www to www for domains :
    ----------------------------------

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

    For www subdomain version to non www subdomain version :
    ----------------------------------------------------------

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.subdomain1\.mydomain\.com [NC]
    RewriteRule ^(.*)$ http://subdomain1.mydomain.com/$1 [R=301,L]

    and

    For Get rid of index.htm :
    -------------------------
    RewriteRule ^index.htm$ http://www.mydomain.com/ [R=301,L]


    So now FIRST issue is, Will Search Engine not treat as spam since I am going to redirect non www version to www version for domains and www version to non www version ( just opposite ) for subdomain.
     
    Jayesh Lodha, Nov 3, 2006 IP
  8. Jayesh Lodha

    Jayesh Lodha Peon

    Messages:
    8
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    So now SECOND issue is, Should I create a seperate .htaccess file for domains and subdomains and put .htaccess file for domains to root folder and .htaccess file for sudomains to http://www.mydomain.com/subdomain1/ folder ( since all files of http://subdomain1.mydomain.com is http://www.mydomain.com/subdomain1/ folder ) ?

    THIRD Issue is, How to add all these three situation in a .htaccess file ?

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

    RewriteEngine on ( Removed in second case )
    RewriteCond %{HTTP_HOST} ^www\.subdomain1\.mydomain\.com [NC]
    RewriteRule ^(.*)$ http://subdomain1.mydomain.com/$1 [R=301,L]

    RewriteRule ^index.htm$ http://www.mydomain.com/ [R=301,L]

    or

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

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

    RewriteRule ^index.htm$ http://www.mydomain.com/ [R=301,L]

    and FORTH and Last Issue is :

    The code mentioned just below will redirect http://www.mydomain.com/index.htm to http://www.mydomain.com/.
    Will it also redirect http://subdomain1.mydomain.com/index.htm to http://subdomain1.mydomain.com/ ?

    RewriteRule ^index.htm$ http://www.mydomain.com/ [R=301,L]

    If answer of second question is no, how to redirect http://subdomain1.mydomain.com/index.htm to http://subdomain1.mydomain.com/
     
    Jayesh Lodha, Nov 3, 2006 IP
  9. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #9
    Each sub domain needs it's own .htaccess file.

    domain.com/.htaccess

    RewriteEngine on
    RewriteRule ^index.htm$ http://www.mydomain.com/ [R=301,L]
    RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

    sub-domain.domain.com/.htaccess

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.subdomain1\.mydomain\.com [NC]
    RewriteRule ^(.*)$ http://subdomain1.mydomain.com/$1 [R=301,L]
     
    Nintendo, Nov 3, 2006 IP
  10. Jayesh Lodha

    Jayesh Lodha Peon

    Messages:
    8
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    So great help from your side. I will never forget you.

    So now your code which you last submitted here, do all of the following.

    http://mydomain.com -> http://www.mydomain.com
    http://mydomain.com/ -> http://www.mydomain.com/
    http://mydomain.com/index.htm -> http://www.mydomain.com
    http://www.sub1.mydomain.com -> http://sub1.mydomain.com
    http://www.sub1.mydomain.com/ -> http://sub1.mydomain.com/
    http://www.sub1.mydomain.com/index.htm -> http://sub1.mydomain.com

    But will your last code do following as well. Or we need to write seperate source code to do to following redirects :

    http://mydomain.com/allfolders/index.htm -> http://www.mydomain.com/allfolders/
    http://mydomain.com/allfiles.htm -> http://www.mydomain.com/allfiles.htm
    http://mydomain.com/allfolders/allfiles.htm -> http://www.mydomain.com/allfolders/allfiles.htm
     
    Jayesh Lodha, Nov 3, 2006 IP
    Jat likes this.
  11. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #11
    It adds www. to the URLs and get's rid of index.htm in the root directory. You don't need to worry about all the index.htm files unless you link to URLs that include them.
     
    Nintendo, Nov 3, 2006 IP
  12. Jayesh Lodha

    Jayesh Lodha Peon

    Messages:
    8
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I have created following files and folders at http://www.brand-voice.com

    http://www.brand-voice.com/index.htm
    http://www.brand-voice.com/file1.htm
    http://www.brand-voice.com/file2.htm
    http://www.brand-voice.com/file3.htm
    http://www.brand-voice.com/folder1/index.htm
    http://www.brand-voice.com/folder1/file1.htm
    http://www.brand-voice.com/folder1/file2.htm

    http://sub1.brand-voice.com/index.htm
    http://sub1.brand-voice.com/file1.htm
    http://sub1.brand-voice.com/file2.htm
    http://sub1.brand-voice.com/file3.htm
    http://sub1.brand-voice.com/folder1/index.htm
    http://sub1.brand-voice.com/folder1/file1.htm
    http://sub1.brand-voice.com/folder1/file2.htm

    http://sub2.brand-voice.com/index.htm
    http://sub2.brand-voice.com/file1.htm
    http://sub2.brand-voice.com/file2.htm
    http://sub2.brand-voice.com/file3.htm
    http://sub2.brand-voice.com/folder1/index.htm
    http://sub2.brand-voice.com/folder1/file1.htm
    http://sub2.brand-voice.com/folder1/file2.htm

    Then created a .htaccess file at root folder for domain

    Source Code is as below :

    RewriteEngine on
    RewriteRule ^index.htm$ http://www.brand-voice.com/ [R=301,L]
    RewriteCond %{HTTP_HOST} ^brand-voice\.com [NC]
    RewriteRule ^(.*)$ http://www.brand-voice.com/$1 [R=301,L]

    Then created a another .htaccess file at http://www.brand-voice.com/sub1/ folder ( since it represents http://sub1.brand-voice.com/ )

    Source Code is as below.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.sub1\.brand-voice\.com [NC]
    RewriteRule ^(.*)$ http://sub1.brand-voice.com/$1 [R=301,L]

    I have not created .htaccess file for sub2 subdomain.

    Then tested and noticed following error :

    http://brand-voice.com/index.htm redirected to http://www.brand-voice.com/ but I got following error "The page isn't redirecting properly".

    http://brand-voice.com/ redirected to http://www.brand-voice.com/ but I got following error "The page is not redirecting properly"

    http://www.sub1.brand-voice.com/index.htm redirected to http://sub1.brand-voice.com/index.htm without error but it should be redirected to http://sub1.brand-voice.com/

    http://www.sub1.brand-voice.com/ redirected successfully.

    So require your help again.

    You know, I am feeling like my big brother is just next to me who is telling me to go ahead, do it. And when I stuck, help me. Thanks a lot for your great help.
     
    Jayesh Lodha, Nov 3, 2006 IP
  13. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #13
    Try just

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^brand-voice\.com [NC]
    RewriteRule ^(.*)$ http://www.brand-voice.com/$1 [R=301,L]

    at

    http://www.brand-voice.com/.htaccess

    the index.htm part might be driving it bonkers. And take the file out of the sub-directory directories.
     
    Nintendo, Nov 3, 2006 IP
  14. Jayesh Lodha

    Jayesh Lodha Peon

    Messages:
    8
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    When I type following URLs in browser and press enter, some urls are redirecting perfectly and some url are not redirecting at all. Even there are some url which is redirecting but to index.htm instead of /

    So here is the urls which I have entered and result when I pressed enter key.

    http://brand-voice.com OK
    http://brand-voice.com/ OK
    http://brand-voice.com/index.htm OK
    http://brand-voice.com/file1.htm OK
    http://brand-voice.com/file2.htm OK
    http://brand-voice.com/file3.htm OK
    http://brand-voice.com/folder1 OK
    http://brand-voice.com/folder1/ OK
    http://brand-voice.com/folder1/index.htm Redirecting to wrong place ( http://www.brand-voice.com/folder1/index.htm )
    http://brand-voice.com/folder1/file1.htm OK
    http://brand-voice.com/folder1/file2.htm OK

    http://www.sub1.brand-voice.com OK
    http://www.sub1.brand-voice.com/ OK
    http://www.sub1.brand-voice.com/index.htm Redirecting to wrong place ( http://sub1.brand-voice.com/index.htm )
    http://www.sub1.brand-voice.com/file1.htm OK
    http://www.sub1.brand-voice.com/file2.htm OK
    http://www.sub1.brand-voice.com/file3.htm OK
    http://www.sub1.brand-voice.com/folder1 OK
    http://www.sub1.brand-voice.com/folder1/ Not Redirecting
    http://www.sub1.brand-voice.com/folder1/index.htm Redirecting to subdomain.domain.com/index.htm instead of subdomain.domain.com/
    http://www.sub1.brand-voice.com/folder1/file1.htm OK
    http://www.sub1.brand-voice.com/folder1/file2.htm OK

    http://www.sub2.brand-voice.com Redirecting to wrong place (http://www.sub2.brand-voice.com/)
    http://www.sub2.brand-voice.com/ Not Redirecting
    http://www.sub2.brand-voice.com/index.htm Not Redirecting
    http://www.sub2.brand-voice.com/file1.htm Not Redirecting
    http://www.sub2.brand-voice.com/file2.htm Not Redirecting
    http://www.sub2.brand-voice.com/file3.htm Not Redirecting
    http://www.sub2.brand-voice.com/folder1 OK
    http://www.sub2.brand-voice.com/folder1/ Not Redirecting
    http://www.sub2.brand-voice.com/folder1/index.htm Not Redirecting
    http://www.sub2.brand-voice.com/folder1/file1.htm Not Redirecting
    http://www.sub2.brand-voice.com/folder1/file2.htm Not Redirecting

    So how to make it work ?
     
    Jayesh Lodha, Nov 4, 2006 IP
  15. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #15
    Nintendo, Nov 4, 2006 IP