Monday, April 25, 2011

How to redirect www to non-www?

I am using URL re-writer module provided by http://urlrewriter.net/ site. Can anyone tell me how can I use there module to redirect www.example.com to example.com (301 redirect)

Thanks

From stackoverflow
  • I've never used urlrewriter, but it looks like you'd use the following (or something similar:

    <redirect url="^(.+)$" to="http://example.com/$1" permanent="true" />
    

    on the www.example.com site.

    abatishchev : It seems that your rule will redirect ANY request, also not domain which is specified. I mean if web site bindings are set to few domains, all of them will be redirected to one given
    lacqui : Ah, yes you're right. I'm used to .htacces, where it only affects the given path and subpaths.
  • <redirect url="http://www.example.com/(.+)$" to="http://example.com/$1">
    
    configurator : Isn't the url only the part after the domain?
    Prashant : not working for me :(
  • If you just want to redirect www.example.com:

    <if header="HTTP_HOST" match="www.example.com">
        <redirect url=".*" to="http://example.com$0" permanent="true" />
    </if>
    

    And if you want to redirect everything except example.com to example.com:

    <unless header="HTTP_HOST" match="example.com">
        <redirect url=".*" to="http://example.com$0" permanent="true" />
    </unless>
    
    Prashant : @Gumbo condition is working for me. is not working. has one little mistake "http://example.com/$0" it should be "http://example.com$0" else it will redirect you from "www.ex.com/dom/about.aspx" to "ex.com//dom/about.aspx" notice the double slashes. Rest its fine, thanks :)
    Prashant : Please edit that slash, for correct answer.

0 comments:

Post a Comment