2

My non-www example.com has been redirected to www.example.com/index.php.

Note: I'm redirecting using .htaccess and not with nginx virtual host file.

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

If I comment the lines in .htaccess and try to redirect through nginx virtual host file curl respond correctly as set but I received a 404 (model by my site) when access www.example.com.

By the way, is it better to redirect this through .htaccess or nginx? As I'm using nginx as proxy.

server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;



location / {
    try_files $uri $uri/ @proxy;
}

location @proxy {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

location ~ \.php$ {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}
}

Virtual Host code Nginx

server {
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}

curl -I example.com

 HTTP/1.1 301 Moved Permanently
 Date: Mon, 04 Dec 2017 20:39:54 GMT
 Content-Type: text/html
 Connection: keep-alive
 Set-Cookie: __cfduid=d7048cb8bebb2c847d904c46cd78d8f0c1512419; 
 expires=Tue, 04-Dec-18 20:39:54 GMT; path=/; domain=.example.com; HttpOnly
 Location: http://www.example.com/
 Server: cloudflare-nginx
 CF-RAY: 3c8193e88245-EWR

All site is working fine but just at the index I receive a 403 Forbidden. Note: it's a internal redirect so I don't know which one nginx or apache is better for performance. In apache non-www is redirect to www.example.com/index.php (I don't want this .index.php and in nginx is redirected normally but I receive a 403 at homepage with the rest of the site working fine.

  • 1
    Please show the "redirect through nginx virtual host file" and the corresponding HTTP response headers. If this is an _external redirect_ then there should be no difference to the `.htaccess` approach. (Except that doing this redirect in Nginx virtual host - at the proxy level - would indeed be the preferred option in terms of performance.) – MrWhite Dec 04 '17 at 19:34
  • I edited my question and put the information asked. Thanks! – Renan Souza Dec 04 '17 at 21:07
  • If Apache is redirecting to `index.php` then it maybe that you've put the redirect in the wrong place? Do you have other directives? Otherwise, the `.htaccess` code you posted would only redirect to `index.php` if `index.php` was on the original request. "it's a internal redirect" - what do you mean? A 3xx response is an _external redirect_. This needs to be an _external redirect_, as it needs to redirect the _client_ to `www` (your server may not even care). – MrWhite Dec 04 '17 at 21:43
  • 1
    Ok, let's forget apache and focus on nginx. Why when I use redirect 301 through virtual host the site works fine but at the home of the site I receive a 403 Forbidden? – Renan Souza Dec 04 '17 at 22:48
  • but answering your question, yes `htaccess` is corret because when I use just apache the non-www redirect to www. without the index.php – Renan Souza Dec 04 '17 at 22:59
  • I'm sure the problem of everything is on the config of the virtual host file. – Renan Souza Dec 04 '17 at 23:31
  • 1
    I founded that when I put the 301 redirect on nginx virtual file the www.example.com get the "default" virtual host which is set to /var/www/ but the other pages for example: www.example.com/testes/ works fine. – Renan Souza Dec 05 '17 at 02:52
  • 1
    Problem solved. I was putting the redirect inside the same block "server { }" and you have to put in a separete block server. – Renan Souza Dec 05 '17 at 14:13
  • Glad you solved the problem. You can add that as an "answer" (and accept it). That way it will benefit other readers. – MrWhite Dec 05 '17 at 16:46

1 Answers1

1

I was putting the redirect inside the same server {} block and I needed to put in a separate server {} block just for the example.com host.

server {
    listen 80;
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}


server {
    listen 80;
    server_name www.example.com;

    location / {
        try_files $uri $uri/ @proxy;
    }

    location @proxy {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ~ \.php$ {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
MrWhite
  • 12,647
  • 4
  • 29
  • 41