Redirect condition/rule for different domain extension

Discussion in 'Apache' started by jacoberoi, Apr 22, 2010.

  1. #1
    I usually write a redirect condition/rule as follows to redirect non-wwws etc:

    
    php_value session.use_only_cookies 1
    php_value session.use_trans_sid 0
    RewriteEngine On 
    RewriteCond %{HTTP_HOST} ^mysite\.co.za [NC] 
    RewriteRule (.*) http://www.mysite.co.za/$1 [R=301,L]
    rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.htm\ HTTP/
    rewriterule ^/index\.htm$ http://www.mysite.co.za/ [R=301,L]
    rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.htm\ HTTP/
    rewriterule index\.htm$ http://www.mysite.co.za/%1 [R=301,L]
    
    Code (markup):
    Now, the problem is that this site also has mysite.com pointing to the same ftp location. I can't get the logins to change the dns settings so I'm trying to solve this with a .htaccess redirect. I works for the .co.za (non-www + /index.htm) but I can't 301 redirect the .com and its non-www + /index.htm to the .co.za. It returns an "internal server error".

    Your help will be much appreciated !!
     
    jacoberoi, Apr 22, 2010 IP
  2. trevHCS

    trevHCS Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'd firstly check for the .com version and redirect that to the .co.za then let it work out the stuff about the .co.za, ie: add this below RewriteEngine On

    RewriteCond %{HTTP_HOST} ^(www.)?mysite\.com [NC]
    RewriteRule (.*) http://www.mysite.co.za/$1 [R=301,L]
    Code (markup):
    ...then when it redirects to the .co.za it should sort the rest of it. At least thats the theory.

    To be honest I'm kindof baffled by some of the other code especially the bits about "\ HTTP/" which I've never come across before. If it doesn't work I'd try commenting out the stuff from THE_REQUEST line downwards and debug from there. I guess most of it makes sense if we knew the exact URLs.

    Trev
     
    trevHCS, May 28, 2010 IP
  3. jacoberoi

    jacoberoi Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the effort Trev!

    The rest of the code redirects non-www urls and /index.htm to the main www url to avoid content duplication.
    Even though your code looks like it should work, it doesn't do the trick. Any ideas, alterations?
     
    jacoberoi, May 31, 2010 IP
  4. trevHCS

    trevHCS Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What version of Apache are you running this on? Just tested it with real domains on Apache 2.2.3 and it seemed to redirect no probs. In that case I used this as the .htaccess:

    
    RewriteEngine On 
    
    RewriteCond %{HTTP_HOST} ^(www.)?carlisle.uk.com [NC] 
    RewriteRule (.*) http://www.ambleside.uk.com/$1 [R]
    
    rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.htm\ HTTP/
    rewriterule ^/index\.htm$ http://www.ambleside.uk.com/ [R=301,L]
    rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.htm\ HTTP/
    rewriterule index\.htm$ http://www.ambleside.uk.com/%1 [R=301,L]
    
    Code (markup):
    That works when I went to either www.carlisle.uk.com or carlisle.uk.com although I've not tested the stuff about index.htm and the likes.

    Main guess currently is that either you're running an earlier Apache that for some unknown reason isn't liking this or it's getting cached somewhere (although really not sure why it'd cache without the redirect).

    I'd double check there aren't any typos in there like missing . or similar. Also sometimes it can complain if you don't backslash the dots, eg: .co.za instead of \.co\.ca and similar.

    Are you using Firefox, Internet Explorer or other to view the page? I know IE sometimes doesn't show the actual redirect in the address bar even though it has followed it. Seems to be better in IE8 as it's quite a security flaw, but certainly IE 7 had that problem. Right pain if you're trying to debug.

    Trev
     
    trevHCS, Jun 1, 2010 IP