Redirecting www to nonwww version on ServerPilot

Posted November 3, 2016 5.1k views
NginxUbuntu 16.04


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} ^$ [NC]
RewriteRule ^(.*)$$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.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers


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}
    RewriteRule (.*)$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.

Thanks this just helped me now. I guess I was using the old code, non-nginx code and it never caused an issue until just an hour ago. :O

But it’s all up and running now that I used that code instead. It was redirecting to /srv/… over and over in the URL box