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.