1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

http://site.com to http://www.site.com using modrewrite?

Discussion in 'Apache' started by EGS, May 12, 2007.

  1. #1
    What's the code to have site.com redirect to www.site.com ?
    I want to use safe 301 redirect using modrewrite.

    Please help me and give me the code. :)
    Thanks!<3
     
    EGS, May 12, 2007 IP
    Zerosleep likes this.
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Place the following in a .htaccess file in the root of your domain (domain.com/.htaccess):
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^domain\.com$
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
    Code (markup):
     
    rodney88, May 12, 2007 IP
  3. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #3
    Is the stuff in the brackets necessary?
     
    EGS, May 12, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What "stuff in brackets"?
     
    rodney88, May 12, 2007 IP
  5. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #5
    [R=301,L]
    Code (markup):
    ^
    That stuff in the brackets..
     
    EGS, May 12, 2007 IP
  6. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes keep it in...
     
    CodyRo, May 12, 2007 IP
  7. login

    login Notable Member

    Messages:
    8,849
    Likes Received:
    349
    Best Answers:
    0
    Trophy Points:
    280
    #7
    Yes, its important. Everybody should use this code in their .htaccess file. It is very good.
     
    login, May 12, 2007 IP
  8. trichnosis

    trichnosis Prominent Member

    Messages:
    13,785
    Likes Received:
    333
    Best Answers:
    0
    Trophy Points:
    300
    #8
    i strongly suggest you to add the code which is given rodney88 on reply two. you must change the url according to your domain. this code will help you to avoid supp. results from site
     
    trichnosis, May 12, 2007 IP
  9. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #9
    Why is that slash there?

    domain[b]\[/b].com
    Code (markup):
    Why's that there? :confused:

    This is one of my .htaccess for one of my sites, and it gets high rankings in Google:

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    rewritecond %{http_host} ^domain.com [nc]
    rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
    Code (markup):
    Can you explain to me what the difference between my code and your code is? As you can see in mine, I have [nc] in brackets (I don't know what it is or what it does) and I have no \ in my domain before the dot (.).

    Please assist. :eek:
     
    EGS, May 12, 2007 IP
  10. login

    login Notable Member

    Messages:
    8,849
    Likes Received:
    349
    Best Answers:
    0
    Trophy Points:
    280
    #10
    Here is mine, I use it on all my sites, I also have a custom 404 page.


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

    ErrorDocument 404 /404.php
     
    login, May 12, 2007 IP
  11. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #11
    The stuff in the brackets are called "flags", [nc] means "no case" which means it's not case sensitive (it will see HeLlO the same as hello).

    The extra \ isn't needed, but it's used as an escape character.. reason is . is a REGEXP function (okay wrong wording bear with me), so to prevent it from being parsed as so the escape character is added so it's just interpreted as a period.

    Probably a bad explanation, but hopefully it answered your question :)
     
    CodyRo, May 13, 2007 IP
  12. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Flags modify the behaviour of the rule -
    R forces a redirect (by default a 302 redirect but we can specify 301 with the =301.
    NC makes it case-insensitive - but the hostname is always lowercase so it's unnecessary here.
    L is for "Last" and forces the rewrite module to stop looking for or applying any more rewrite rules - once you've sent out your 301 moved header, you don't want to do any more rewriting.

    Just to expand a bit on CodyRo's explanation - in a regex expression the dot is a metacharacter (special character). It will match any character at all, and is used in that sense in the next line for the RewriteRule itself. But when we're checking the hostname, we want a literal period and not the wildcard metacharacter (which would match against any character). To get a literal period we simply escape it's special properties with the backslash.

    Obviously it still works without being escaped since a period falls into the category of "any character".
     
    rodney88, May 13, 2007 IP
    EGS likes this.
  13. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #13
    Thanks for the explanation guys. Rep added. :)
    Just for further clarification...nc is not needed? :confused:

    I guess not huh, since it's just being used for the redirect?
     
    EGS, May 13, 2007 IP
  14. pociteh

    pociteh Peon

    Messages:
    52
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Does the change take effect immediately?

    I've just pasted the code to my .htaccess, but I don't notice any change. I have changed the 'domain' part to my domain, but when I access http://mydomain.com it isn't redirected to http://www.mydomain.com. I'm wondering whether this is normal.
     
    pociteh, May 13, 2007 IP
  15. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #15
    Is your domain a .com.. :p
     
    EGS, May 13, 2007 IP
  16. inworx

    inworx Peon

    Messages:
    4,860
    Likes Received:
    201
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Replace the word your domain.com WITH your ORIGINAL DOMAIN AND TLD

    Yes, the change takes place immedaitely
     
    inworx, May 13, 2007 IP
  17. pociteh

    pociteh Peon

    Messages:
    52
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Yes, my domain is a .com.
    I tried it again just now and it works. I don't precisely know why it didn't work yesterday, since what I did just now is the same as what I did yesterday (and I've saved the file after modifying via the file manager in cpanel). But weirdly, when I opened the file today, all the modifications I pasted there yesterday was gone, so I did the same thing again and... it works.

    Thanks for the help! :)
     
    pociteh, May 13, 2007 IP
  18. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #18
    Maybe you just forgot to save it after you modified it. :p I do that a lot, and then get all angry with any errors thinking I did not make an error, because I am perfect. :)
     
    EGS, May 14, 2007 IP
  19. inworx

    inworx Peon

    Messages:
    4,860
    Likes Received:
    201
    Best Answers:
    0
    Trophy Points:
    0
    #19
    No one is perfect.
     
    inworx, May 14, 2007 IP
  20. adultuserbars

    adultuserbars Peon

    Messages:
    762
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #20
    The change takes place immediately to new visitors to your site, but your ISP/computer may cache your site and you don't see the results unless the cache has been cleared.
     
    adultuserbars, May 15, 2007 IP