SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Guru
    Join Date
    Jan 2010
    Posts
    633
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Redirect to Avoid Duplicate Sites

    Sitepoint Members,
    Is
    RewriteCond %{HTTP_HOST} ^xyz.com
    RewriteRule ^(.*)$ http://www.xyz.com/$1 [r=301,L]

    still the best way to handle the www, as opposed to a 301 redirect?

    Do you think I should redirect to www or redirect to no www. I think I'm going to redirect to No www, it's a cleaner look, "www" is not so new anymore.

    If so, then I would have to use:
    RewriteCond %{HTTP_HOST} ^www.xyz.com
    RewriteRule ^(.*)$ http://xyz.com/$1 [r=301,L]

    right?

    Thanks,

    Chris

  2. #2
    Mouse catcher silver trophy Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,881
    Mentioned
    122 Post(s)
    Tagged
    1 Thread(s)
    @Chris77 - I've moved this to the "Server Configuration, Apache & URL Rewriting" forum where hopefully you'll get better answers.

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Chris,

    Quote Originally Posted by Chris77 View Post
    Sitepoint Members,
    Is
    RewriteCond %{HTTP_HOST} ^xyz.com
    RewriteRule ^(.*)$ http://www.xyz.com/$1 [r=301,L]

    still the best way to handle the www, as opposed to a 301 redirect?

    No. Use
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^xyz\.com [NC]
    RewriteRule .? http://www.xyz.com%{REQUEST_URI} [R=301,L]
    as explained in my signature's tutorial.

    Do you think I should redirect to www or redirect to no www. I think I'm going to redirect to No www, it's a cleaner look, "www" is not so new anymore.

    I, too, prefer non-www but that's in the tutorial article, too.

    If so, then I would have to use:
    RewriteCond %{HTTP_HOST} ^www.xyz.com
    RewriteRule ^(.*)$ http://xyz.com/$1 [r=301,L]

    right?

    No. Pretty much as above.

    Thanks,

    Chris
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    SitePoint Guru
    Join Date
    Jan 2010
    Posts
    633
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    dklynn,
    For www entered into a browser and changed to no www,

    code like this?
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.xyz\.com [NC]
    RewriteRule .? http://xyz.com%{REQUEST_URI} [R=301,L]


    Thanks,


    Chris

  5. #5
    SitePoint Guru
    Join Date
    Jan 2010
    Posts
    633
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    dklynn,
    I put in
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.xyz.com
    RewriteRule ^(.*)$ http://xyz.com/$1 [r=301,L]

    and entering a www address into a browser stayed at a www address.

    Chris
    Last edited by Chris77; Feb 12, 2012 at 18:55. Reason: spelling

  6. #6
    SitePoint Guru
    Join Date
    Jan 2010
    Posts
    633
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    dklynn,
    I tried that again like this

    RewriteCond %{HTTP_HOST} ^www.xyz\.com [NC]
    RewriteRule .? http://xyz.com%{REQUEST_URI} [R=301,L]

    and it had no effect.

    Chris

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Chris,

    First, escape the dot character in your {HTTP_HOST} regex.

    Second, that would have no effect on the mod_rewrite code so it's likely that:

    1. mod_rewrite is not enabled. If this were the case, though, you'd have received a 505 error message (because the mod_rewrite code would not have been understood.

    2. Your host has not enabled the use of .htaccess on your server.

    3. You are being hosted on an M$ server (i.e., not Apache).

    To be sure, read the part in my tutorial about setting up a server and TESTING that mod_rewrite is enabled and functioning. Run the simple test then come back with the results.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  8. #8
    SitePoint Guru
    Join Date
    Jan 2010
    Posts
    633
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    dklynn,
    What I eneded up getting to work was

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

    Chris

  9. #9
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Chris,

    Same thing except slower because my .? version sending the %{REQUEST_URI} is the SAME as capturing the {REQUEST_URI} to $1 and adding that to the domain redirection. Six of one, half dozen of the other as it's only slightly more than a matter of style.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  10. #10
    SitePoint Guru
    Join Date
    Jan 2010
    Posts
    633
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If it's faster I'd like to use it but you gave me no www to www and I need www to no www. I tried to write it with your help but could get it to work.

    How about this:

    RewriteCond %{HTTP_HOST} ^www\.xyz\.com [NC]
    RewriteRule .? http://xyz.com%{REQUEST_URI} [R=301,L]

    Thanks,

    Chris

  11. #11
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Chris,

    Ahhhhh, perfection!

    Oh, I've assumed that you'll retain the previous RewriteEngine on statement.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  12. #12
    SitePoint Guru
    Join Date
    Jan 2010
    Posts
    633
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    DK,

    I put

    RewriteCond %{HTTP_HOST} ^www\.xyz\.com [NC]
    RewriteRule .? http://xyz\.com%{REQUEST_URI} [R=301,L]

    in the htaccess file with that slash (red) because earlier you said something about closing the dot.

    Should that slash be there? The code is working with the slash.

    Thanks,

    Chris

  13. #13
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Chris,

    Whoops! There should be NO backslashes in the redirection, ONLY IN {regular expressions}. In that code, the RewriteCond's ^www\.xyz\.com$ is regex and .? in the RewriteRule is regex.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  14. #14
    SitePoint Guru
    Join Date
    Jan 2010
    Posts
    633
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So
    RewriteCond %{HTTP_HOST} ^www\.xyz\.com [NC]
    RewriteRule .? http://xyz\.com%{REQUEST_URI} [R=301,L]

    Shold be?
    RewriteCond %{HTTP_HOST} ^www\.xyz\.com$ [NC]
    RewriteRule .? http://xyz.com%{REQUEST_URI} [R=301,L] (no \)


    Thanks,

    Chris

  15. #15
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Chris,

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.xyz\.com$ [NC]
    # match {HTTP_HOST} start-of-string www dot xyz dot com end-of-string No Case
    RewriteRule .? http://xyz.com%{REQUEST_URI} [R=301,L]
    # match anything and permanently redirect to http://xyz.com with the same request ({REQUEST_URI} string)
    Yup, it's as easy as that!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  16. #16
    SitePoint Guru
    Join Date
    Jan 2010
    Posts
    633
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    DK,
    That works fine, although it didn't look like it worked at first because IE holds on to its cached copy much more that FF. You have to use Ctrl F5 regularly with IE when testing pages.

    Thanks for the help,

    Chris

  17. #17
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    IE is known as a PITA - but mostly for the lack of DOM compliance. Yes, cache on IE is another major problem (when testing).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •