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

1 htaccess to rule them all (www to non-www)

Discussion in 'Server Configuration and Hosting' started by Adam Howard, Jun 18, 2014.

  1. Adam Howard

    Adam Howard Well-Known Member

    So for my own use, I'm trying to make my htaccess as generic as possible (so I don't have to maintain more than one)

    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}^on(s)|
    RewriteRule http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    I got the above from here
    This forwards non-www to www.

    The advantage is I don't have to actually supply the domain name. Thus it works everywhere (cool, eh?).

    What I'd like is away to reverse it ... ie... force www to non-www.
  2. Brogan

    Brogan XenForo Moderator Staff Member

    It can vary from server to server.

    I had to change my rewrite when I moved from shared to dedicated.
    SneakyDave likes this.
  3. Adam Howard

    Adam Howard Well-Known Member

    So you don't know how to reverse that?
  4. nodle

    nodle Well-Known Member

    This is for www to non-www:
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
    RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
    But like @Brogan said above, it may vary server to server.
  5. Adam Howard

    Adam Howard Well-Known Member

    Problem with that redirect is you have to add the domain name. In the one above (the one I posted), I don't need to do so.
  6. Tracy Perry

    Tracy Perry Well-Known Member

    If you had of searched a little more on StackOverFlow you would have found this ;)
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    I make no promise that it works since I wouldn't touch Apache.
    SneakyDave and Adam Howard like this.
  7. Adam Howard

    Adam Howard Well-Known Member

    That redirect non-www to www

    I'm looking for it the other way around... ie.... www to non-www
  8. Tracy Perry

    Tracy Perry Well-Known Member

    SneakyDave likes this.
  9. SneakyDave

    SneakyDave Well-Known Member

    I'm a little teapot, short and stout.
    Last edited: Jun 18, 2014
  10. Tracy Perry

    Tracy Perry Well-Known Member

    Well, considering he specifically mentioned .htaccess we would assume so. :p

    All folks of finer distinction run nginx. :LOL:
    SneakyDave likes this.
  11. Adam Howard

    Adam Howard Well-Known Member

    For now, yes, Apache is the concern.

    At a later time, I'll need the nginx alternatives to this. ;)

    I believe I've modified it correctly to do what the original does (only without www)
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteCond %{HTTPS}^on(s)|
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    Thank you @Tracy Perry
  12. RoldanLT

    RoldanLT Well-Known Member

    I'm using this when I'm still on shared host:
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 
    Edit: It's non www to www ;)
    SneakyDave likes this.
  13. Adam Howard

    Adam Howard Well-Known Member

    Huh, this seems to have had 1 negative affect.

    Is now being redirected as

    Notice the word /forums/ is missing.
  14. Tracy Perry

    Tracy Perry Well-Known Member

    Well, if you aren't running in a subdirectory that would be correct on the second one - for the threads the forums should not be showing.
    Screen Shot 2014-06-19 at 12.29.22 AM.png

    If you are running in a subdirectory then you may need the rewritebase defined.

    And that looks like what is the problem because your normal string is
    SneakyDave likes this.
  15. Adam Howard

    Adam Howard Well-Known Member

    I'm using the default htaccess provided by XenForo. I've only added 3 working lines of code

    #    enable cross-origin support
    Header set Access-Control-Allow-Origin "*"
    #    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 500 default

    IfModule mod_rewrite.c>
    RewriteEngine On
    #    Redirect www to non www
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$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} -[OR]
    RewriteCond %{REQUEST_FILENAME} -[OR]
    RewriteCond %{REQUEST_FILENAME} -d
    ^.*$ - [NC,L]
    RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]
  16. Tracy Perry

    Tracy Perry Well-Known Member

    I draw your attention to
    #    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
    Try uncommenting that (and doing the necessary edit) and see if it resolves it.
  17. Adam Howard

    Adam Howard Well-Known Member

    Yep, already changed that to
    RewriteBase /forums
    No joy
  18. Adam Howard

    Adam Howard Well-Known Member

    I'm wondering if this would be easier to just switch from /forums/ into document root .....
  19. Tracy Perry

    Tracy Perry Well-Known Member

    Why... it's working like it's supposed to... you are confusing yourself because you have it in a subdirectory called forums apparently. This is what shows when you go into a thread (and it is what should show if you have your forum in a subdirectory called /forums).
    Screen Shot 2014-06-19 at 12.53.41 AM.png

    Attachment replaced with another one to prevent a wee-wee match from starting.
    SneakyDave likes this.
  20. Adam Howard

    Adam Howard Well-Known Member

    I'm aware of that.

    But the old WWW links do not redirect correctly

    originally with WWW
    Now without www
    Place that into your browser and notice the difference :(

Share This Page