• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 1.1 www vs. non-www domain [logging isssues]

TheBigK

Well-known member
#1
I've found out that our XF board does not handle the www and non-www logging-in properly. Let's say a user enters: domain.com and logs in; and then clicks a URL that begins with www.domain.com/threads/.... , then the new page that opens has already logged out the user.

I know that technically, www and non www domains are technically two different addresses. But is there a way this can be fixed?

Say, even if the user enters domain.com, he/she should be directed to the www version? I'd like to enforce www in my domain addresses.
 

TheBigK

Well-known member
#2
Addendum:

1. Does this have any affect on the SEO?
2. Will forcing www in domain name have any issues with the subdomains currently existing OR in future?
 

Sadik B

Well-known member
#3
Put this in your site root's .htaccess file


Code:
RewriteEngine On
 
RewriteCond %{HTTP_HOST} !(^www\.domain\.com$|^cdn\.domain\.com$)
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]
  1. Replace domain with your own domain.
  2. The example above considers the case of subdomains. cdn.domain.com is a subdomain. Similarly you can add more sub-domains like this
    Code:
    |^xyz\.domain\.com$
  3. This will not impact SEO because the URLs are 301 redirected.
  4. It is better to make sure all your internal URLs are generated as www
 

TheBigK

Well-known member
#4
Thank you, Sadik. I didn't get sub-domains part. Will I have to create a new addition to the htaccess for each new domain that I'll add?
 

Sadik B

Well-known member
#5
Thank you, Sadik. I didn't get sub-domains part. Will I have to create a new addition to the htaccess for each new domain that I'll add?

It's like this

RewriteEngine On

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

Basically what this is doing is looking at the hostname in the url and rewriting all urls to begin with www except when the url is one of the listed subdomain exceptions. abc, def, xyz etc are all subdomains you might have.
 

TheBigK

Well-known member
#8
Our boards reside in /community/ (XF) and WP in main domain. Where exactly should we add the rules?

WordPress seems to be redirecting to www version automatically. But not XF.

----------------
Noticed something strange: If I manually type domain name without 'www', then all the forum links are non-www. If I use www, then they automatically get www in the forum names.

Is this expected?
 

Jake Bunce

XenForo moderator
Staff member
#9
Our boards reside in /community/ (XF) and WP in main domain. Where exactly should we add the rules?
It should go in the .htaccess file in your /community directory. Add the red code to your .htaccess file inside of the /community directory. This assumes you are currently using XF's default .htaccess file:

Code:
#	Mod_security can interfere with uploading of content such as attachments. If you
#	cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
#	SecFilterEngine Off
#	SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default

<IfModule mod_rewrite.c>
	RewriteEngine On

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

	#	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 /xenforo

	#	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|crossdomain\.xml|robots\.txt) - [NC,L]
	RewriteRule ^.*$ index.php [NC,L]
</IfModule>
That will enforce no www for all forum pages. Obviously you need to specify your domain name in the rewrite rules.
 

CTXMedia

Formerly CyclingTribe
#13
Try this:

Code:
RewriteCond %{HTTP_HOST} ^crazyengineers\.com$ [NC]
RewriteRule ^(.*)$ http://www.crazyengineers.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/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
        RewriteRule ^.*$ index.php [NC,L]
</IfModule>
 

MattW

Well-known member
#19
This is what I have in mine to force www

Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[0-9]+(\.[0-9]+){3} [OR]
RewriteCond %{HTTP_HOST} ^mail [OR]
RewriteCond %{HTTP_HOST} ^z22se.co.uk
RewriteRule (.*) http://www.z22se.co.uk/$1 [R=301,L]
 

TheBigK

Well-known member
#20
I'm afraid to try this out because every time I'm trying a rule; something or the other breaks and my users aren't appreciating it.

Can someone offer a concrete fix? I don't think this is a hard to crack problem. But surprisingly, even my hosting guys couldn't fix it!