.htaccess not working for Ghost Droplet

August 13, 2015 3k views
Nginx Ghost

I'm having difficulty running my .htaccess file within my Ghost droplet. I have an .htaccess file with the following commands to redirect non-www requests to www requests, but a redirect is not happening:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

I also tried the nginx server workaround, but got a continuous loop error with the redirect script and have a CNAME with www and mywebsitedomain. Any thoughts as to why this redirect isn't happening?

1 comment
  • Hi,
    Please try out the following command -

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

    Add the above code to you .htaccess file and replace domain.com with your domain name.

4 Answers

As @jsamuel mentioned, the Ghost One-Click droplet uses the Nginx web server which doesn't use .htaccess files. In order to direct non-www requests to www, you can update Nginx's configuration. One the Ghost droplet, you can find that in /etc/nginx/sites-enabled/ghost You'll want to add a new server block that returns a 301 redirect from the non-www domain to the www one. It would look like this:

server {
    listen 80;
    server_name example.com;

    return 301 http://www.example.com$request_uri;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name www.example.com;

    client_max_body_size 10G;

    location / {
        proxy_pass http://localhost:2368;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
    }
}

I believe the Ghost server image uses only Nginx, not Nginx in front of Apache, so you can't use Apache's .htaccess files. You'd have to make your changes in the Nginx configuration.

Hey @asb thanks for the explanation. I add your snippet so my file looks like this,

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name mydomain.com; # Replace with your domain

    return 301 http://www.mydomain.com$request_uri;



    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 10G;

    location / {
        proxy_pass http://localhost:2368;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
    }
}

and the redirect occurs, but I get an error

This webpage has a redirect loop

ERR_TOO_MANY_REDIRECTS

Any reason why I have that issue based on my file code above?

In terms of the DNS setup, I have an "A" file directing the IP to my domain without www. and a "CNAME" file directing my domain to www.

Not sure if that helps

  • @connordphillips There need to be two seperate server blocks in the Nginx configuration, just like in the one I posted above.

  • @asb thank you for the explanation. Is there any reason why your configuration does not include: root /usr/share/nginx/html;
    index index.html index.htm;

    That comes in my nginx configuration by default

  • Those are just part of the default configuration. It wouldn't hurt to have them, but they aren't strictly needed in this case.

Sorry @asb it worked as you had said. Sorry for so many questions and thank you for your help!

Have another answer? Share your knowledge.