Question

.htaccess not working for Ghost Droplet

Posted August 13, 2015 6k views
NginxGhost

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

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.

×
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

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

Submit an Answer