0

I am not able redirect my example.com request to www.example.com and they are now both different websites, pointing to the same application.

Background:

I have installed php 7 with nginx on Openshift ver. 2 using this. I have added 2 aliases for the application deployed, as follows:

  1. Application URL: myapp.rhcloud.com; Domain Name: www.example.com
  2. Application URL: myapp.rhcloud.com; Domain Name: example.com

I have also added the following DNS detailes in CNAME:

  1. Name: www.example.com; Value: myapp.rhcloud.com
  2. Name: @; Value: www.example.com

My nginx config file (erb) is as follows:

server {
    root              <%= ENV['OPENSHIFT_REPO_DIR'] %>/www;
    listen            <%= ENV['OPENSHIFT_PHP_IP'] %>:<%= ENV['OPENSHIFT_PHP_PORT'] %>;
    server_name       <%= ENV['OPENSHIFT_APP_DNS'] %>;
    index             index.php index.html index.htm <%= ENV['NGINX_EXTRA_INDEX'] %>;
    set_real_ip_from  <%= ENV['OPENSHIFT_PHP_IP'] %>;
    real_ip_header    X-Forwarded-For;

which becomes:

server {
    root              /path/to/my/app-root/folder/;
    listen            127.11.2.1:8080;
    server_name       myapp.rhcloud.com;
    index             index.php index.html index.htm ;
    set_real_ip_from  127.11.2.1;
    real_ip_header    X-Forwarded-For;

I have tried to add another server block in my nginx config file like the following:

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

However I am not sure what should I use in place of example.com.

2 Answers2

0

I don't use Openshift but it seems to me that there is something wrong in your DNS settings where you've created a CNAME for your root domain:

Name: @; Value: www.example.com

According to DNS specifications you cannot create CNAME record for your root domain. Even if your DNS server allow you to do that, it will not work.

Check out this question to see why is that not allowed.

madz
  • 123
0

As far as DNS goes, as pointed out by another answer, you cannot do @ CNAME www, i.e., cannot point from the root to a subdomain. However, doing the opposite, www CNAME @, i.e., pointing from a subdomain back to the root, should be entirely acceptable, and does not have to be symmetric to the way the web-side of the domain is represented (e.g., web-site-wise, you could still point from root to a subdomain, even though you're doing the exact opposite on the DNS side).

As far as the web-site way is concerned, it would seem that it would make sense to have both server definitions in the same file, where you simply manually prefix www. right in front of <%= ENV['OPENSHIFT_APP_DNS'] %> for one of the servers, e.g., server_name www.<%= ENV['OPENSHIFT_APP_DNS'] %>;.

Alternatively, although it wouldn't be the most efficient solution, you could also do a conditional redirect with an if statement in a single server context (e.g., one with server_name .example.com;):

if ($host != "www.example.com") {
    rewrite ^   http://www.example.com$request_uri? redirect;
}

Alternatively, if you would rather prefer pretty but less efficient code, you could also use the regular expressions:

if ($host !~ "^www\.") {
    rewrite ^   http://www.$host$request_uri?   redirect;
}
cnst
  • 14,646