How to assign different separate domain for a Drupal page using Nginx

A client asked me to work on one particular page of his existing Drupal site to have its own different separate domain. This is one good solution if you have special web page that you want to quickly improve ranking on search engines.

Lets say, the http://www.webfoobar.com/node/88 is the page that we want it to have its own domain "test7.webfoobar.com". The following steps will accomplish this task:

  1. The following Nginx configuration will get the rendered output of http://www.webfoobar.com/node/88 page to pass it as a response output when test7.webfoobar.com domain is requested. If you have PageSpeed running and its Trim URLs filter is enabled, PageSpeed needs to be disabled to avoid broken web assets. The query parameter PageSpeed=off that you will find in the following Nginx configuration will disable the PageSpeed feature (Note: you can remove it if the PageSpeed is not enabled or installed). While query parameter getpagefordomain=1 serves as flag that the Drupal page is requested by its own assigned different domain. And the following Nginx configuration will redirect the http://www.webfoobar.com/node/88 page to http://test7.webfoobar.com if requested.

          
    server {
      listen 80;
      server_name test7.webfoobar.com;
    
      access_log off;
      log_not_found off;
      error_log /var/log/nginx_error.log error;
    
      location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header Host "www.webfoobar.com";
        proxy_pass http://www.webfoobar.com/?q=node/88&PageSpeed=off&getpagefordomain=1;
      }
    }
    
    server {
      listen 80;
      server_name www.webfoobar.com webfoobar.com;
    
      access_log off;
      log_not_found off;
      error_log /var/log/nginx_error.log error;
    
      location = /node/88 {
        if ($arg_getpagefordomain != 1) {
          return 301 http://test7.webfoobar.com;
        }
        try_files $uri $uri/ @cache;
      }
    }
          
        

    In this article, you will find the complete Nginx configuration or shall we say continuation to follow the named location @cache from above Nginx codes.

  2. The page requested is not guaranteed that all links and web static files' paths are in absolute URL and this will result 404 or Not Found errors. This article will guide to alter Drupal final HTML output to change all paths from relative to absolute URL.

  3. Another issue that will encounter if a website is trying to access web static files from different URL domain is the web static files are blocked by CORS policy: No 'Access-Control-Allow-Origin' header issue. Follow this article to solve this issue.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.