Redirecting www to nonwww version on ServerPilot

November 3, 2016 109 views
Nginx Ubuntu 16.04

Hi,

I am totally new to having my own server. So please bear with me if my questions are too simple for some of you.

I used ServerPilot to set everything up. Everything works great, but my www version doesn't redirect to non www.

I have this code in my .htaccess file (which worked perfectly on shared hosting before - I assume because they were using Apache):

RewriteCond %{HTTP_HOST} ^www.example.com.se$ [NC]
RewriteRule ^(.*)$ http://example.com.se/$1 [R=301,L]

Now, my research shows that serverpilot uses nginx and that the above code doesn't work for nginx. But then I read a guide on serverpilot that says they use nginx along with apache and that the htaccess code should work. But it is not working currently.

So I am very confused. I would appreciate any help on how to achieve this.

1 Answer

Hi,

What you're doing as well as those rewrite rules look correct. ServerPilot configures Nginx as a reverse proxy in front of Apache so you still can use .htaccess files exactly as you're doing.

Please check to make sure you've added those RewriteRule/RewriteCond lines to the very top of your .htaccess file. If you've added them below other stuff (for example, below the WordPress stuff), they won't work reliably.

  • Thanks for taking the time to help out. I appreciate it.

    Turns out the code I was using (even though it worked on my old shared hosting server) had to be slightly changed. I ended up taking the exact code from ServerPilot's guide article and pasting that in my .htaccess and it worked instantly.

    Basically the new code is slightly different than the old one with some character differences. For anyone who may face the same problem in the future, here is the correct code:

    
    RewriteCond %{HTTP_HOST} =www.example.com
    RewriteRule (.*) http://example.com/$1 [R=302,L]
    
    

    Notice the few missing characters from my original code above (like "se$" and "^") and of corse the 301 instead of 302.

Have another answer? Share your knowledge.