XF 2.0 Forwarding non-www traffic to www traffic

Bonsai Coder

Active member
I had this set up in XenForo 1.x and it worked fine. Now it is no longer working(?) I am looking for a simple redirect so that all non-www traffic is forwarded to www. For example if someone enters:

https://bonsainut.com, I would like them to be forwarded to https://www.bonsainut.com.

My previous fix involved adding this code to my htaccess.txt:

Code:
RewriteEngine On
        RewriteCond %{HTTPS_HOST} ^bonsainut\.com$
        RewriteRule ^(.*)$ "https\:\/\/www\.bonsainut\.com\/$1" [R=301,L]

Any thoughts? SSL works with both www and non-www traffic.
 
The backslashes and quotes shouldn't be needed. Try this...
Code:
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^bonsainut.com [NC]
RewriteRule ^(.*)$ https://www.bonsainut.com/$1 [R=301,L]
 
Last edited:
The backslashes and quotes shouldn't be needed. Try this...

Thank you for your assistance! Sadly, it is not working for me. Here is my entire htaccess.txt 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 405 default
ErrorDocument 406 default
ErrorDocument 500 default
ErrorDocument 501 default
ErrorDocument 503 default

<IfModule mod_rewrite.c>
    RewriteEngine On
       
        RewriteCond %{HTTPS} off
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

        RewriteCond %{HTTP_HOST} ^bonsainut.com [NC]
        RewriteRule ^(.*)$ https://www.bonsainut.com/$1 [L,R=301]
       
    #    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>

Not sure what the issue is...
 
I'm not aware that you should change the .htaccess file at all when upgrading from xf1 to xf 2, just leave your old one in place as is.

htaccess.txt is fine to leave there as it does nothing. I'm not actually sure why it's in the xenforo upgrade package at all
 
I'm not aware that you should change the .htaccess file at all when upgrading from xf1 to xf 2, just leave your old one in place as is.

htaccess.txt is fine to leave there as it does nothing. I'm not actually sure why it's in the xenforo upgrade package at all
Yes, it is fine to leave. But, if there is one on his server (.htaccess) it's not working for his redirect and he's editing the wrong file.

He should be editing .htaccess, not htaccess.txt.
 
Yes, it is fine to leave. But, if there is one on his server (.htaccess) it's not working for his redirect and he's editing the wrong file.

He should be editing .htaccess, not htaccess.txt.

I agree absolutely, what confuses me is why, after an upgrade, the existing .htaccess which was working before with xf1 is not working now. The upgrade doesn't touch that file.
 
Thanks all for the help... my problem is often that I edit one of these files, and then two years go by... and I forget what I did :)

So here is the current text of my .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 405 default
ErrorDocument 406 default
ErrorDocument 500 default
ErrorDocument 501 default
ErrorDocument 503 default

<IfModule mod_rewrite.c>

    RewriteEngine On

        RewriteCond %{HTTPS} off
        RewriteRule ^ https://%{HTTPS_HOST}%{REQUEST_URI} [L,R=301]

        RewriteCond %{HTTP_HOST} ^bonsainut.com [NC]
        RewriteRule ^(.*)$ https://www.bonsainut.com/$1 [L,R=301]

    #    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 %{HTTPS} off
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^.*$ - [NC,L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^.*$ index.php [NC,L]

</IfModule>

/EDIT Nevermind! I got it to work - I had a one letter typo hiding in there :) I edited the code above to remove the extra char so that no one refers to it and makes a mistake. The code above DOES work. Thank you for all your assistance!!
 
Last edited:
Try changing it to this...

Code:
<IfModule mod_rewrite.c>

    RewriteEngine On

    #    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 %{HTTPS} off
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

        RewriteCond %{HTTP_HOST} ^bonsainut.com [NC]
        RewriteRule ^(.*)$ https://www.bonsainut.com/$1 [L,R=301]

        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^.*$ - [NC,L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^.*$ index.php [NC,L]

</IfModule>
 
Actually it's not.

HTTP isn't forwarding to HTTPS. But non-www is forwarding to www.

Post the current content so it can be looked at again.
 
Change it to this..

Code:
#    SecFilterEngine Off
#    SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 405 default
ErrorDocument 406 default
ErrorDocument 500 default
ErrorDocument 501 default
ErrorDocument 503 default

<IfModule mod_rewrite.c>

    RewriteEngine On

        RewriteCond %{HTTPS} off
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

        RewriteCond %{HTTP_HOST} ^bonsainut.com [NC]
        RewriteRule ^(.*)$ https://www.bonsainut.com/$1 [L,R=301]

    #    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 %{HTTPS} off
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^.*$ - [NC,L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule ^.*$ index.php [NC,L]

</IfModule>

That should fix the HTTP to HTTPS problem.

If not, I'm out of ideas. :(
 
Last edited:
Back
Top Bottom