Help using .htaccess to redirect multiple versions of domain.

Discussion in 'Apache' started by Komoti, Apr 8, 2008.

  1. #1
    Hi, I am trying to figure out a way to rewrite multiple versions of a domain name and redirect to a single domain using .htaccess.

    I would like to rewrite http://domain.com, www.domain.com and domain.com (including a trailing slash if entered) to http://www.domain.com/.

    I basically need a rule to match everything preceding the domain.com and redirect to http://www.domain.com/. I was able to do this by creating a new rewrite condition for each version but I'm sure there is a simpler way based on my experience with PHP's regexp function rules.

    I have tried multiple tutorials and searched these forums but I can't seem to get this to work so any help is appreciated.
     
    Komoti, Apr 8, 2008 IP
  2. buldozerceto

    buldozerceto Active Member

    Messages:
    1,137
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #2
    Place this above every rewrite rule you have:
    
    RewriteCond %{HTTP_HOST} ^domain\.com$
    RewriteRule ^(.*)$ http://www.domain.com/$1 [QSA,L]
    
    Code (markup):
     
    buldozerceto, Apr 8, 2008 IP
  3. Komoti

    Komoti Member

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Thanks a lot buldozerceto, it worked and was as simple as I could ask for.
     
    Komoti, Apr 8, 2008 IP
  4. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #4
    How do I redirect from ?id=33, I would normally use something like :

    RedirectMatch permanent ^/how_to_hypnotize_someone.php$ http://www.site.com/how_to_bewilder_someone.htm
    Code (markup):
    But I've converted an asp site and want to redirect all my old site.net/?id=33 type urls to my new article.html type urls and the above method doesn't want to play?
     
    Astroman, Apr 9, 2008 IP