Mod rewrite domain.com/abc to abc.domain.com ?

Discussion in 'Apache' started by Kerosene, Jun 25, 2007.

  1. #1
    Google has picked up a few of my subdomains but is using domain.com/abc to link to them. How can I rewrite these back to abc.domain.com to avoid duplicate content issues?
     
    Kerosene, Jun 25, 2007 IP
  2. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #2
    You can try a 301 redirect like this:
    
    Redirect 301 /abc http://abc.domain.com
    
    Code (markup):
     
    VimF, Jun 27, 2007 IP
    Kerosene likes this.
  3. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #3
    Thanks - that nearly works.
    But it messes up the rest of the URL.

    I need to change
    www.domain.com/subdomain/search+term
    to
    subdomain.domain.com/search+term
    Code (markup):
     
    Kerosene, Jun 28, 2007 IP
  4. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #4
    How does the rest of it look like?

    I have the very same setup on one of my site's subdomain and it works perfectly.
     
    VimF, Jun 28, 2007 IP
  5. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #5
    Kerosene, Jun 28, 2007 IP
  6. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #6
    Well then remove all of the rewrites in the domain.com/subdomain/, keep only the 301 redirect.

    The rewrites won't be used there anyway.
     
    VimF, Jun 28, 2007 IP
  7. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #7
    But that's exactly why I've got that stuff in the original htaccess... I want to avoid index.php?s=bla+bla+bla and just have /bla+bla+bla

    -----

    I found this in another forum, I have absolutely no idea what's going on, but it works.
    Can somebody check to make sure that all the 301s etc are correct?
    I am WAY out of my depth with mod_rewrite (obviously!)

    htaccess for domain.com/subdomain:
    RewriteEngine On
    
    #my original rewrite to make the url a bit prettier
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d 
    RewriteRule ^([^/\.]+)/?$ index.php?s=$1 [QSA,L]
    
    #new stuff to rewrite directory to subdomain
    RewriteRule !\..{3,4}$ - [C]
    RewriteCond %{HTTP_HOST} !^subdomain
    RewriteCond %{REQUEST_URI} !^.*/$
    RewriteRule ^(.+)$ http://subdomain.domain.com/ [R=301,L]
    
    RewriteCond %{HTTP_HOST} !^subdomain
    RewriteRule ^(.*)$ http://subdomain.domain.com/$1 [R]
    Code (markup):
    Can anyone tidy this up or improve on it?
     
    Kerosene, Jun 28, 2007 IP
  8. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #8
    You don't need the rewrite code in the old directory.

    Everything (/bla+bla+bla) will be redirect to the new subdomain, place the rewrite code there.
     
    VimF, Jun 28, 2007 IP
  9. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #9
    There has only ever been one subdomain directory - there is no 'old' directory. I created the subdomain in cPanel, which creates a directory domain.com/subdomain.

    domain.com/subdomain/.htaccess is the only htaccess file in use. If I remove the original rewrite from it, my site will break.
     
    Kerosene, Jun 28, 2007 IP
  10. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #10
    Okay :) So mod_rewrite redirect would be the perfect solution.
     
    VimF, Jun 28, 2007 IP
  11. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #11
    I still don't really understand what you're talking about :confused:

    domain.com/subdomain/this+is+a+search
    and
    subdomain.com/this+is+a+search
    both work at the moment.

    All I need to do, is make it so
    domain.com/subdomain/this+is+a+search
    301s to
    subdomain.com/this+is+a+search
    to avoid duplicate content issues.
     
    Kerosene, Jun 28, 2007 IP
  12. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #12
    Nup. that doesn't work.

    I'll just stick with this unless somebody can clean it up a bit.
    RewriteEngine On
    
    #my original rewrite to make the url a bit prettier
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d 
    RewriteRule ^([^/\.]+)/?$ index.php?s=$1 [QSA,L]
    
    #new stuff to rewrite directory to subdomain
    RewriteRule !\..{3,4}$ - [C]
    RewriteCond %{HTTP_HOST} !^subdomain
    RewriteCond %{REQUEST_URI} !^.*/$
    RewriteRule ^(.+)$ http://subdomain.domain.com/ [R=301,L]
    
    RewriteCond %{HTTP_HOST} !^subdomain
    RewriteRule ^(.*)$ http://subdomain.domain.com/$1 [R]
    Code (markup):
     
    Kerosene, Jun 28, 2007 IP
  13. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Your first block of code rewrites /stuff to index.php?s=stuff. Your second block of code then redirects whatever the current request is (now, after the rewrite, index.php?s=) to the subdomain.

    You just need to change the order round - do the redirect first, then the rewrite:
    Redirect 301 /abc http://abc.domain.com
    
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d 
    RewriteRule ^([^/\.]+)/?$ index.php?s=$1 [QSA,L]
    
    Code (markup):
     
    rodney88, Jun 29, 2007 IP
  14. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #14
    I tried that, it nearly works, but the url gets screwed.
    e.g
    
    http://www.domain.com/subdomain/my+search+term
    goes to:
    http://subdomain.domain.com/my+search+term?s=my+search+term
    Code (markup):
    Any ideas?

    I'm happy to use the messy htaccess that I posted above, it works - but I'm pretty sure it's not an elegant way to do it.
     
    Kerosene, Jun 29, 2007 IP
  15. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #15
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
    RewriteRule ^(.*)$ http://abc.domain.com/$1? [R=301,L]
    
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d 
    RewriteRule ^([^/.]+)/?$ index.php?s=$1 [QSA,L]
    Code (markup):
    I dunno how to stop it preserving the query string when using mod_alias, but hopefully that should do it using mod_rewrite.
     
    rodney88, Jun 29, 2007 IP
    Kerosene likes this.
  16. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #16
    It works!

    Man what a saga! Thanks everyone :)

    [​IMG]
     
    Kerosene, Jun 29, 2007 IP
    Silver89 likes this.