• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
  • 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

Puntocom

Well-known member
#1
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.
 
#2
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]
 

Steve F

Well-known member
#4
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]
 

Hoffi

Well-known member
#5
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]
 
#7
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 .
 

Anthony Parsons

Well-known member
#8
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.
 
#9
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>
 

sforum

Active member
#10
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)