1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

My .htaccess file to redirect non-www to www

Discussion in 'Tips and Guides [Archive]' started by Puntocom, Nov 20, 2010.

  1. Puntocom

    Puntocom Well-Known Member

    Hi, if you don't redirect non-www to www (or viceversa), example.com and www.example.com will be different sites with duplicated content and the automatic login will not work.

    I have made this modification to my .htaccess file to redirect non-www to www. I searched www redirect and I didn't find this, I hope it helps:

    Change:

    Code:
    <IfModule mod_rewrite.c>
            RewriteEngine On
    
            RewriteCond %{REQUEST_FILENAME} -f [OR]
            RewriteCond %{REQUEST_FILENAME} -l [OR]
            RewriteCond %{REQUEST_FILENAME} -d
            RewriteRule ^.*$ - [NC,L]
            RewriteRule ^(data|js|styles|install) - [NC,L]
            RewriteRule ^.*$ index.php [NC,L]
    </IfModule>
    
    to:

    Code:
    <IfModule mod_rewrite.c>
            RewriteEngine On
    
            RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
            RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    
            RewriteCond %{REQUEST_FILENAME} -f [OR]
            RewriteCond %{REQUEST_FILENAME} -l [OR]
            RewriteCond %{REQUEST_FILENAME} -d
            RewriteRule ^.*$ - [NC,L]
            RewriteRule ^(data|js|styles|install) - [NC,L]
            RewriteRule ^.*$ index.php [NC,L]
    </IfModule>
    
    replacing example.com for your domain name.
     
    bibitbunga, Alit, UKD and 6 others like this.
  2. smsmasters

    smsmasters Active Member

    Thanks it worked but I had to specify the full forum path in mine.
    For some reason it didn't include it when redirecting.

    Code:
    RewriteCond %{HTTP_HOST} ^smsmasters\.co.uk$ [NC]
    RewriteRule ^(.*)$ http://www.smsmasters.co.uk/forum/$1 [R=301,L]
     
    hpidriver and Puntocom like this.
  3. Brogan

    Brogan XenForo Moderator Staff Member

    I had to do a similar thing for mine, redirecting to non-www.
    The code used here on XenForo.com didn't work on my server/domain.
     
    smsmasters likes this.
  4. Steve F

    Steve F Well-Known Member

    What exactly did you use Brogan? I thought my .htaccess was correct but checking now I am not redirected to the non-www version. Here is the part I have.

    Code:
    RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.example\.com$
    RewriteRule ^/?$ "http\:\/\/example\.com\/community\/index\.php" [R=302,L]
     
  5. Hoffi

    Hoffi Well-Known Member

    You only rewrite completely without subdomain URL. If you want to rewrite all subdomains that point to ths directory you better can use this one.

    rewrite to www.
    Code:
            RewriteCond %{HTTP_HOST} !^www.example\.com$ [NC]
            RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L,QSA]
    or to non-www
    Code:
            RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
            RewriteRule ^(.*)$ http://example.com/$1 [R=301,L,QSA]
     
    tafreehm and mrGTB like this.
  6. Brogan

    Brogan XenForo Moderator Staff Member

    This is the only way it works on my server.

    Code:
    RewriteCond %{HTTP_HOST} www.cliptheapex.com$
    RewriteRule ^(.*)$ http://cliptheapex.com/$1 [R=301,L]
     
    51463, Tim Jay, arms and 1 other person like this.
  7. Adam H

    Adam H Member

    Im having real issues with this one, Ive tried basically every combination possible to get this working. I have other forums and sites on the server including 3 VB forums which all work with the below code but i cant get it working with Xen. Any ideas would be fantastic.

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


    Thanks in advance guys .
     
  8. Anthony Parsons

    Anthony Parsons Well-Known Member

    Mine uses:

    RewriteCond %{HTTP_HOST} ^domain\.org$ [NC]
    RewriteRule ^(.*)$ http://www.domain.org/c/$1 [R=301]

    You should also have this as near the first thing, after rewrite being turned on, as location itself is enough for it not to work if its being processed after other rewrite conditions.
     
    Sal Collaziano likes this.
  9. Adam H

    Adam H Member

    Got it working with this, Not sure what i done differently from all the other times as i think ive tried this one before but ill post it here in case someone else wants to see it :

    Code:
    <IfModule mod_rewrite.c>
        RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.yourdomain\.co.uk$ [NC]
    RewriteRule ^(.*)$ http://www.yourdomain.co.uk/$1 [R=301,L,QSA]
        #    If you are having problems with the rewrite rules, remove the "#" from the
        #    line that begins "RewriteBase" below. You will also have to change the path
        #    of the rewrite to reflect the path to your XenForo installation.
        RewriteBase /
     
        #    This line may be needed to enable WebDAV editing with PHP as a CGI.
        #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
     
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^(data/|js/|styles/|install/|favicon\.ico) - [NC,L]
        RewriteRule ^.*$ index.php [NC,L]
     
    </IfModule>
     
    GeeksChat likes this.
  10. sforum

    sforum Active Member

    i'm thinking of installing xenforo in a dir called /xf/ rather than in the root of my domain, which means i need http://www.mydomain.com and http://mydomain.com to redirect to http://www.mydomain.com/xf/

    how would i best achieve this? or is there a reason why i should not install xenforo in a sub-directory and should just install it in the root?

    (i'm just thinking ahead; if, one day, my site expands beyond a simple forum, i might want to separate the different app's rather than having one dominate the root folder)
     

Share This Page