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

Redirecting non www. to www?

Discussion in 'XenForo Questions and Support' started by KenX, Aug 19, 2012.

  1. KenX

    KenX Member

    I am trying to redirect my forum to http://www.example.com but it isn't working.I edited my .htaccess and added this code
    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^domain.com
    RewriteRule ^ http://www.domain.com%{REQUEST_URI} [L,R=301]
    But still it didn't work.Need help.
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Add the red code to the .htaccess file in your forum root. The second line must include the name of the forum directory. If your forum is in the web root then just remove the /forum part:

    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} !^www\.yoursite\.com$
    	RewriteRule ^(.*)$ http://www.yoursite.com/forum/$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>
    
     
  3. KenX

    KenX Member

    Nope,it still hasn't worked.
     
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    I can take a look if you give me your URL and FTP info.
     
  5. KenX

    KenX Member

    I asked my host for help and they have done it for me.
    Case closed.
     
  6. Robby

    Robby Well-Known Member

    It doesn't work for me, can you take a look plz: forums.hidemychat.com
     
  7. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    You are using a subdomain. Do you want to enforce that subdomain? What should be redirected to what?
     
  8. sinful

    sinful Member

    Hi,

    if i would like to rewrite to non-www.

    whats the diff between using [NC]
    Code:
    Code:
    RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L,QSA]
    and without [NC] ?
    Code:
    Code:
    RewriteCond %{HTTP_HOST} !^example\.com$
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L,QSA]
    Also, what does the QSA in [R=301,L,QSA] do?
     
    Last edited: Aug 22, 2013
  9. Brogan

    Brogan XenForo Moderator Staff Member

    sinful likes this.
  10. sinful

    sinful Member

  11. lazy llama

    lazy llama Well-Known Member

    The info about QSA can be found on the same page that Brogan linked to.
     
  12. sinful

    sinful Member

    Saw it but being a computer noob, could someone please explain what that means in less technical terms? :sick:
     
  13. lazy llama

    lazy llama Well-Known Member

    If a URL has a query string at the end of it , e.g. http://www.somesite.com/search.php?highlight=yes , then the default action when rewriting that URL would be to lose the "?" and anything after it.

    Adding QSA tags the bit after the "?" on to the rewritten URL.
     
    sinful likes this.
  14. sinful

    sinful Member

    Code:
        RewriteCond %{HTTP_HOST} !^main.com$ [NC]
        RewriteRule ^(.*)$ http://main.com/$1 [R=301,L]
    The above code works fantastically. However I just added a Addon Domain (e.g. addon.com), uploading the addon.com website files to public_html/addon

    Problem with the above code now is that whenever someone accesses addon.com, it gets redirected to main.com/addon. Is there a way to prevent this? I found out the only way to prevent this is get rid of the above code. But when I do that, I cannot redirect www. to non-www.

    Lastly, to prevent duplicates, these 4 entry points to the addon-domain needs to be redirected back to the main domain.
    e.g.
    addon.main.com
    www.addon.main.com
    main.com/addon
    www.main.com/addon

    All 4 has to redirect to main.com


    Anyone know how to achieve all this through .htaccess ?

    Many thanks!
     
  15. lazy llama

    lazy llama Well-Known Member

    Code:
    RewriteCond expr " %{HTTP_HOST} -strcmatch '*.main.com$'"
    Rewrite ^(.*)$ http://main.com/$1 [R=301,L]
    RewriteCond expr " %{HTTP_HOST} -strcmatch '*.addon.com$'"
    Rewrite ^(.*)$ http://addon.com/$1 [R=301,L]
    
    For the final set of redirects, some are covered by the above but you would also need to add this:
    Code:
    RewriteCond %{HTTP_HOST} ^main.com$
    RewriteCond %{REQUEST_URI} ^/addon/$
    Rewrite ^(.*)$ http://main.com/ [R=301,L]
    
     
    Last edited: Sep 6, 2013
    sinful likes this.
  16. sinful

    sinful Member

    Thanks for the prompt reply!

    Just to confirm, I shall replace the previous .htaccess edits
    Code:
    RewriteCond %{HTTP_HOST} !^main.com$ [NC]
    RewriteRule ^(.*)$ http://main.com/$1 [R=301,L]
    with this:
    Code:
    RewriteCond expr " %{HTTP_HOST} -strcmatch '*.main.com$'"
    Rewrite ^(.*)$ http://main.com/$1 [R=301,L]
    RewriteCond expr " %{HTTP_HOST} -strcmatch '*.addon.com$'"
    Rewrite ^(.*)$ http://addon.com/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} ^main.com$
    RewriteCond %{REQUEST_URI} ^/addon/$
    Rewrite ^(.*)$ http://main.com/ [R=301,L]
    
    ?
     
  17. lazy llama

    lazy llama Well-Known Member

    I think so, yes. Take a back copy of the existing .htaccess in case you need to put it back.
    Not been able to test it myself as I no longer use Apache, but it looks like it should work.

    Ideally instead of doing this in .htaccess, it should be done using multiple <virtualhost> directives in the Apache config, which makes it simpler as you use the virtual host ServerName or ServerAlias to match the different domains and then only apply the re-write rules that apply to that domain. But I'm guessing you only have access to the .htaccess files.
     
    sinful likes this.
  18. sinful

    sinful Member

    Just tried it and it gave an Internal Server Error :censored:

    hmmm
     
  19. lazy llama

    lazy llama Well-Known Member

    Okay, let's try it step by step.

    Replace the original rewrite with
    Code:
    RewriteCond %{HTTP_HOST} !^main.com$ [NC]
    RewriteCond %{HTTP_HOST} !^addon.com$ [NC]
    RewriteRule ^(.*)$ http://main.com/$1 [R=301,L]
    That should redirect only things which don't match http://main.com or http://addon.com
     
    sinful likes this.
  20. sinful

    sinful Member

    Thanks.

    Just tried these. Works as you mentioned.

    Main.com URLs are good. And,

    www.addon.main.com
    addon.main.com
    www.main.com/addon

    --> all these 3 gets redirected to main.com/addon/


    Whats left is to ensure

    www.addon.com does not get redirected to
    and addon.com
    points to add.com
    and not get redirected to
    main.com/addon/

    also

    www.addon.main.com
    addon.main.com
    www.main.com/addon

    redirects to main.com
     
    Last edited: Sep 6, 2013

Share This Page