Forum Moderators: phranque

Message Too Old, No Replies

url redirection only on exact match of domain, not if url is present

HTTP_HOST redirection using mod_rewrite on exact match of domain

         

shib4u

3:57 pm on Apr 21, 2011 (gmt 0)

10+ Year Member



I want mod rewrite rules on the following conditions...
  • Main domain gets redirected to www
  • Sub-domain gets redirected to www if nothing is specified in the url

    e.g.

    example.com gets redirected to www.example.com
    example.com/123.htm gets redirected to www.example.com/123.htm

    sub.example.com gets redirected to www.example.com
    sub.example.com/123.htm does NOT get redirected to www.example.com (i.e. stays as it is)
  • g1smd

    5:40 pm on Apr 21, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    What code have you tried so far?

    What results did you get and how did those differ from what you expected?

    shib4u

    6:17 pm on Apr 21, 2011 (gmt 0)

    10+ Year Member



    for the first part of the example... this works...
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteCond %{HTTP_HOST} !^$
    RewriteRule ^/(.*) http://www.example.com/$1 [L,R]

    I'm not able to get to do the second part.

    g1smd

    6:27 pm on Apr 21, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Your code says "if hostname part of the request does not begin with www.example.com then redirect to www.example.com with a 302 redirect".

    The code will not redirect non-canonical www requests that happen to include a port number.

    The code works for all paths and files as specified by the (.*) part of the pattern.

    With a leading slash in the RegEx pattern, your code will work inside httpd.conf but not inside .htaccess.

    shib4u

    7:17 pm on Apr 21, 2011 (gmt 0)

    10+ Year Member



    Hi g1smd, thanks for explaining. The next part for rewrite which i want is....

    subdomain.example.com gets redirected to www.example.com
    subdomain.example.com/123.htm does NOT get redirected to www.example.com (i.e. stays as it is)

    shib4u

    7:51 pm on Apr 21, 2011 (gmt 0)

    10+ Year Member



    RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
    RewriteRule (.*) [mydomain.com...] [P]

    ?

    jdMorgan

    6:08 pm on Apr 25, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Something like:

    # Code for use only in server config file, outside any <Directory> containers (will not work in .htaccess or inside a <Directory> container):
    #
    # If non-www hostname is requested
    RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
    # or if www.example.com hostname is requested with appended FQDN token and/or port number
    RewriteCond %{HTTP_HOST} ^www\.example\.com(\.|\.?:[0-9]+)$ [NC,OR]
    # or if any hostname other than www.example.com is requested with no additional URL-path
    Rewritecond %{HTTP_HOST}/$1 !^www\.example\.com/.+$
    # then externally redirect to www.example.com, retaining the originally-requested URL-path
    RewriteRule ^/(.*) http://www.example.com/$1 [R=301,L]

    For use in a top-level .htaccess file shared among multiple subdomains, remove the leading slash from the RewriteRule pattern.

    Jim

    shib4u

    12:55 pm on Apr 27, 2011 (gmt 0)

    10+ Year Member



    Thanks for the pointers Jim... i have finally implemented the following...

     RewriteEngine On
    Rewritecond %{HTTP_HOST}/$1 ^static\.example\.com [NC,OR]
    Rewritecond %{HTTP_HOST}/$1 ^origin.\stat\.example\.com [NC,OR]
    Rewritecond %{HTTP_HOST}/$1 ^stat\.example\.com [NC]
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^/(.*) http://www.example.com/$1 [L,R]


    Note that my non-www to www is not in the above, since that VHost is in a different config, so that was not added in this block.