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

editing .htaccess for www redirect

Discussion in 'Server Configuration and Hosting' started by Pride1760, Nov 6, 2011.

  1. Pride1760

    Pride1760 Member

    Hi,

    please help me redirect non-www domain to www.

    This is what my .htaccess currently looks like:

    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
     
        #    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) - [NC,L]
        RewriteRule ^.*$ index.php [NC,L]
    </IfModule>
    I understand this is the code required for redirecting non-www to www
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L]
    Now where exactly do I insert this? I tried adding it at the bottom, but the site won't redirect.

    Thanks!
     
  2. Brogan

    Brogan XenForo Moderator Staff Member

  3. Slavik

    Slavik XenForo Moderator Staff Member

    At the top.

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.example.come$ [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
     
    #    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
     
        #    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) - [NC,L]
        RewriteRule ^.*$ index.php [NC,L]
    </IfModule>
     
  4. Pride1760

    Pride1760 Member

    got it...

    thanks guys for the help!
     
  5. mrGTB

    mrGTB Well-Known Member

    That's wrong, you've got "RewriteEngine on" listed unnecessary 2 times now.

    One:
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.example.come$ [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    Two:
    Code:
        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
    All you need do is put it here, so it's also included in the "<IfModule mod_rewrite.c>" section for checking if active, along with the other RULES and CON listed there checked.

    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
     
        #    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
     
        #    Redirect with or without WWW code goes here.
        RewriteCond %{HTTP_HOST} ^www.example.come$ [NC]
        RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
     
        #    This line may be needed to enable WebDAV editing with PHP as a CGI.
        #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
     
  6. mrGTB

    mrGTB Well-Known Member

    And this is what I use:

    Now in proper code format:

    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
     
        #    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
     
        #    Redirect code used to always include WWWW in URL address.
        RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
        RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
     
        #    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>
    Read this page on Askapache: http://www.askapache.com/htaccess/modrewrite-tips-tricks.html
     
    Pride1760 likes this.
  7. Eagle

    Eagle Well-Known Member

    Well if forum moved to /forum/ folder?
     
  8. Eagle

    Eagle Well-Known Member

    Well it should be like this;

    Code:
        RewriteCond %{HTTP_HOST} !^www.site\.com$ [NC]
        RewriteRule ^(.*)$ http://www.site.com/forum/$1 [R=301,L,QSA]
    
     

Share This Page