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

XF 1.4 .htaccess redirect

Discussion in 'XenForo Questions and Support' started by el canadiano, Apr 24, 2015.

  1. el canadiano

    el canadiano Active Member

    Recall that the default XenForo's .htaccess looks like this.

    #    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
    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
        Options +FollowSymlinks -MultiViews
        #    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]
    Given my domain is https://rockbandforums.com, I want to extend my .htaccess so that it does the following.
    Now, the third bullet point is impossible because my key is only valid for rockbandforums.com without the www. Therefore, we will ignore the last point.

    Now, here's my issue. I have the following code which satisfies the first two points.

    RewriteCond %{HTTP_HOST} !^www\.rockbandforums\.com$
    RewriteRule ^(.*)$ https://rockbandforums.com/$1 [R=301,L]
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://rockbandforums.com/$1 [R=301,L]
    From what I have tested, if I were to place this after XenForo's conditions, this will not work at all. If I place it before, then it will work. However, XenForo's friendly URLs feature (which is currently turned on) will break.

    Is there a way where I can rewrite my .htaccess such that it will work with both the friendly URLs and get that to write?
  2. el canadiano

    el canadiano Active Member

    Or is there a way to implement that on the XenForo level perhaps?
  3. Mike

    Mike XenForo Developer Staff Member

    I would note that the first rule would actually match your 1st and 3rd bullet points. The second rule is what handles the 2nd point.

    Conceptually, the rules are ok, but this line is wrong:
    RewriteCond %{HTTP_HOST} !^www\.rockbandforums\.com$
    I believe it should be:
    RewriteCond %{HTTP_HOST} !^rockbandforums\.com$
    You need to enter the host that you want to be the canonical version.

    These rules need to go before the default XF rules as the last XF rule is a catch-all.
  4. el canadiano

    el canadiano Active Member

    Brilliant as you once was when I last used the software. Correct me if I'm wrong, but your updated cond is saying if it isn't rockbandforums.com? I've been using nginx the last little while and this is the first time back on Litespeed.
  5. Mike

    Mike XenForo Developer Staff Member

    Correct. If it isn't the version without the www, redirect to that URL.
    Alex S and el canadiano like this.

Share This Page