A more than usual RewriteCond! Help please.

Discussion in 'Apache' started by killjoy, May 9, 2009.

  1. #1
    Okay... a new idea for people, I bet everyone has gone to type a website, www.domain.co.uk and only typed two w's i.e. ww.domain.co.uk or even double pressed the key to get four w's i.e. wwww.domain.co.uk.

    So I want to help the visitors to my websites that do this easy mistake.

    In my DNS I have added ww. and wwww. to my C name.

    In apache I have added the following into the Apache conf in the VirtualHost...
    ServerName domain.co.uk
    ServerAlias ww.domain.co.uk
    ServerAlias www.domain.co.uk
    ServerAlias wwww.domain.co.uk

    All of these are setup and working, you can enter ww.domain.co.uk and it loads the site.

    At the moment the only bit of kit I have setup is for if someone types http://domain.co.uk/ it will send them to http://www.domain.co.uk/

    Can someone please help me to change...

    RewriteCond %{HTTP_HOST} ^domain\.co\.uk
    RewriteRule ^(.*) http://www.domain.co.uk/$1 [L,R=301]

    To also rewrite ww.domain.co.uk to www.domain.co.uk and wwww.domain.co.uk to www.domain.co.uk

    I already thought about adding a new line of...
    RewriteRule ^http://ww.domain.co.uk/$ http://www.domain.co.uk/ [R=301,L]

    But that wont work if someone types in ww.domain.co.uk/page.php

    Can someone please help with this RewriteCond!?

    Many Thanks.
     
    killjoy, May 9, 2009 IP
  2. SSANZ

    SSANZ Peon

    Messages:
    861
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SSANZ, May 10, 2009 IP
  3. killjoy

    killjoy Well-Known Member

    Messages:
    201
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thanks,

    I got it working by doing the following...

    RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [NC]
    RewriteRule ^(.*) http://www.domain.co.uk/$1 [L,R=301]
    RewriteCond %{HTTP_HOST} ^ww\.domain\.co\.uk$ [NC]
    RewriteRule ^(.*) http://www.domain.co.uk/$1 [L,R=301]
    RewriteCond %{HTTP_HOST} ^wwww\.domain\.co\.uk$ [NC]
    RewriteRule ^(.*) http://www.domain.co.uk/$1 [L,R=301]

    Does anyone know how to correctly use the [OR] command to tidy up this code?
     
    killjoy, May 10, 2009 IP
  4. vx|brian

    vx|brian Peon

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    vx|brian, May 10, 2009 IP
  5. killjoy

    killjoy Well-Known Member

    Messages:
    201
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Thanks, got it finished by doing...

    RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^ww\.domain\.co\.uk$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^wwww\.domain\.co\.uk$ [NC]
    RewriteRule ^(.*) http://www.domain.co.uk/$1 [L,R=301]
     
    killjoy, May 11, 2009 IP