We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12410
    • 353 Posts
    MODX Revolution 2.4.0-pl

    Hi Guys how can I alter the code at https://docs.modx.com/revolution/2.x/getting-started/installation/basic-installation/nginx-server-config
    ...to redirect from example.com to www.example.com

    The code:
    server {
            listen 80;
            server_name example.com www.example.com;
            root /home/sites/example.com;
            index index.php;
            client_max_body_size 30M;
            location / {
                    root /home/sites/example.com;
                    if (!-e $request_filename) {
                            rewrite ^/(.*)$ /index.php?q=$1 last;
                    }
            }
            location ~ \.php$ {
                    try_files $uri =404;
                    fastcgi_split_path_info ^(.+\.php)(.*)$;
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                    include fastcgi_params;
                    fastcgi_ignore_client_abort on;
                    fastcgi_param  SERVER_NAME $http_host;
            }
     
            location ~ /\.ht {
                    deny  all;
            }
    }


    I've tried adding:
    if ($host = "example.com") {
        rewrite ^ $scheme://www.example.com$request_uri permanent;
    }
    location / {
        try_files $uri $uri/ @modx-rewrite;
    }


    ...from https://support.modx.com/hc/en-us/articles/217295387-Nginx-Web-Rules-Rewrites ...but no luck as I get the error "ERR_TOO_MANY_REDIRECTS" in Chrome.

    and also tried

     return 301 $scheme://www.example.com$request_uri;


    ...from https://www.nginx.com/blog/creating-nginx-rewrite-rules/ ...but no luck as I get the error "ERR_TOO_MANY_REDIRECTS" in Chrome.

    Any ideas please?

    Thanks



      • 12410
      • 353 Posts
      Ok so I can get example.com to go to www.example.com but now how do I get example.com/test redirect to www.example.com/test

      # 
      
      server {
      	listen 80 default_server;
      	listen [::]:80 default_server ipv6only=on;
      
      	root /var/www/example.com/html;
      	index index.php index.html index.htm;
      
      	
      	server_name example.com;
      
      	 client_max_body_size 30M;
              location / {
                      root /var/www/example.com/html;
                      if (!-e $request_filename) {
                              rewrite ^/(.*)$ /index.php?q=$1 last;
                       }
                      if ($host = "example.com") {
                     rewrite ^ $scheme://www.example.com$request_uri permanent;
                       }
                    try_files $uri $uri/ @modx-rewrite;
                 }
              location ~ \.php$ {
                      try_files $uri =404;
                      fastcgi_split_path_info ^(.+\.php)(.*)$;
                      fastcgi_pass unix:/var/run/php5-fpm.sock; 
                      fastcgi_index  index.php;
                      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                      include fastcgi_params;
                      fastcgi_ignore_client_abort on;
                      fastcgi_param  SERVER_NAME $http_host;
              }
       
              location ~ /\.ht {
                      deny  all;
              }
      
      	
      }
      
      
        • 12410
        • 353 Posts
        Ok this works if it helps others

        
        server {
        	listen 80 default_server;
        	listen [::]:80 default_server ipv6only=on;
        
        	root /var/www/example.com/html;
        	index index.php index.html index.htm;
        
        	# Make site accessible from http://localhost/
        	server_name example.com www.example.com;
        
        	 client_max_body_size 30M;
                location / {
                        root /var/www/example.com/html;
                        if ($host = "example.com") {
                       rewrite ^ $scheme://www.example.com$request_uri permanent;
                         }
                        if (!-e $request_filename) {
                                rewrite ^/(.*)$ /index.php?q=$1 last;
                         }
                         try_files $uri $uri/ @modx-rewrite;
                         }
        
                location ~ \.php$ {
                        try_files $uri =404;
                        fastcgi_split_path_info ^(.+\.php)(.*)$;
                        fastcgi_pass unix:/var/run/php5-fpm.sock; 
                        fastcgi_index  index.php;
                        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                        include fastcgi_params;
                        fastcgi_ignore_client_abort on;
                        fastcgi_param  SERVER_NAME $http_host;
                }
         
                location ~ /\.ht {
                        deny  all;
                }
        
        	
        }