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

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)

PHP:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

I got the above from here
http://stackoverflow.com/questions/4916222/htaccess-how-to-force-www-in-a-generic-way
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.
 
This is for www to non-www:
Code:
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.
 
This is for www to non-www:
Code:
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.
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.
 
If you had of searched a little more on StackOverFlow you would have found this ;)
Code:
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.
 
If you had of searched a little more on StackOverFlow you would have found this ;)
Code:
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.
That redirect non-www to www

I'm looking for it the other way around... ie.... www to non-www
 
Well, considering he specifically mentioned .htaccess we would assume so. :p

All folks of finer distinction run nginx. :LOL:
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)
PHP:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Thank you @Tracy Perry
 
I'm using this when I'm still on shared host:
Code:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Edit: It's non www to www ;)
 
Huh, this seems to have had 1 negative affect.

PHP:
http://www.sociallyuncensored.eu/forums/threads/aol-american-online-discovers-security-breach-during-spam-investigation.18676/

Is now being redirected as

PHP:
http://www.sociallyuncensored.eu/threads/aol-american-online-discovers-security-breach-during-spam-investigation.18676/

Notice the word /forums/ is missing.
 
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.webp

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

EDIT:
And that looks like what is the problem because your normal string is
Code:
http://sociallyuncensored.eu/forums/forums/general-population.43/
 
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.
View attachment 76143

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

EDIT:
And that looks like what is the problem because your normal string is
Code:
http://sociallyuncensored.eu/forums/forums/general-population.43/
I'm using the default htaccess provided by XenForo. I've only added 3 working lines of code

PHP:
#    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
#</IfModule>

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} -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>
 
I draw your attention to
Code:
#    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.
 
I draw your attention to
Code:
#    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
Yep, already changed that to
PHP:
RewriteBase /forums
No joy
 
I'm wondering if this would be easier to just switch from /forums/ into document root .....
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.webp

EDIT:
Attachment replaced with another one to prevent a wee-wee match from starting.
 
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).
I'm aware of that.

But the old WWW links do not redirect correctly

originally with WWW
PHP:
http://www.sociallyuncensored.eu/forums/threads/aol-american-online-discovers-security-breach-during-spam-investigation.18676/

Now without www
PHP:
http://sociallyuncensored.eu/forums/threads/aol-american-online-discovers-security-breach-during-spam-investigation.18676/

Place that into your browser and notice the difference :(
 
Top Bottom