1. 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]

Discussion in 'Troubleshooting and Problems' started by TheBigK, Jun 24, 2012.

  1. TheBigK

    TheBigK Well-Known Member

    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.
     
  2. TheBigK

    TheBigK Well-Known Member

    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?
     
  3. Sadik B

    Sadik B Well-Known Member

    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
     
    tigapoin likes this.
  4. TheBigK

    TheBigK Well-Known Member

    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?
     
  5. Sadik B

    Sadik B Well-Known Member


    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.
     
  6. GencnesilleR

    GencnesilleR Member

    sadikb

    Proceed to login to the site when we would like site.com and not www.site.com. I could not do with these codes. We will help right?
     
  7. Sadik B

    Sadik B Well-Known Member

  8. TheBigK

    TheBigK Well-Known Member

    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?
     
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
     
    Carlos Oporto, tigapoin and Chris D like this.
  10. TheBigK

    TheBigK Well-Known Member

    Jake, I actually want to enforce 'www' for all forum pages. That is, I want all the pages should begin with www.crazyengineers.com/community/...
     
  11. Lost

    Lost Well-Known Member

    www. is soooo 1990's :whistle:
     
  12. TheBigK

    TheBigK Well-Known Member

    Old is gold.

    Anyway; I just tried Sadik's suggestion and found out that if I type crazyengineers.com/community/ , I get redirected to www.crazyengineers.com ! Not what I want.
     
    Jarod likes this.
  13. CyclingTribe

    CyclingTribe Well-Known Member

    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>
    
     
  14. CyclingTribe

    CyclingTribe Well-Known Member

    Also, as an aside - the login issue is because the XF cookie is set for the domain where you login and www.yoursite.com is a different domain to yoursite.com insofar as your login cookie is concerned. (y)
     
  15. TheBigK

    TheBigK Well-Known Member

    Right, but I need to make sure that both Google Spiders and People get uniform experience. Will try your suggestion now.
     
  16. TheBigK

    TheBigK Well-Known Member

    @clickfinity: That does not work. If I strip out www from the URL, I get a 404 page.
     
  17. MattW

    MattW Well-Known Member

    Does the server your site is on have the mod_rewrite module installed?
     
  18. TheBigK

    TheBigK Well-Known Member

    Yep.
     
  19. MattW

    MattW Well-Known Member

    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]
    
     
  20. TheBigK

    TheBigK Well-Known Member

    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!
     

Share This Page