Redirect to 'www' prefix domain using nginx The 2019 Stack Overflow Developer Survey Results Are InRedirect to https with www for particulate domain on HAproxyHow do I redirect different apps into a single domain with Nginx Ubuntu 16Redirect all remaining pages using location blockCan anybody detect file in directory www (nginx)?Getting 404 error in Ubuntu//nginx using IP addressCan't redirect HTTP to HTTPS on NGINXNginx failing to redirect to docker containerRuby on Rails - 403 Forbidden - Ownership / Permissions - DocumentRoot doesn't exist - Denied by Server Configurationnginx expose port and not redirectNGINX - Change request_method & redirect

What does ひと匙 mean in this manga and has it been used colloquially?

Return to UK after being refused entry years previously

Why hard-Brexiteers don't insist on a hard border to prevent illegal immigration after Brexit?

How can I autofill dates in Excel excluding Sunday?

Why was M87 targetted for the Event Horizon Telescope instead of Sagittarius A*?

How are circuits which use complex ICs normally simulated?

Are there incongruent pythagorean triangles with the same perimeter and same area?

Right tool to dig six foot holes?

Resizing object distorts it (Illustrator CC 2018)

Why did Acorn's A3000 have red function keys?

Is flight data recorder erased after every flight?

Does the shape of a die affect the probability of a number being rolled?

The difference between dialogue marks

Deal with toxic manager when you can't quit

Protecting Dualbooting Windows from dangerous code (like rm -rf)

Write faster on AT24C32

Why do we hear so much about the Trump administration deciding to impose and then remove tariffs?

Is a "Democratic" Oligarchy-Style System Possible?

Which Sci-Fi work first showed weapon of galactic-scale mass destruction?

What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?

How technical should a Scrum Master be to effectively remove impediments?

Loose spokes after only a few rides

Am I thawing this London Broil safely?

How to deal with fear of taking dependencies



Redirect to 'www' prefix domain using nginx



The 2019 Stack Overflow Developer Survey Results Are InRedirect to https with www for particulate domain on HAproxyHow do I redirect different apps into a single domain with Nginx Ubuntu 16Redirect all remaining pages using location blockCan anybody detect file in directory www (nginx)?Getting 404 error in Ubuntu//nginx using IP addressCan't redirect HTTP to HTTPS on NGINXNginx failing to redirect to docker containerRuby on Rails - 403 Forbidden - Ownership / Permissions - DocumentRoot doesn't exist - Denied by Server Configurationnginx expose port and not redirectNGINX - Change request_method & redirect



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I brought a domain in godady and I have Installed Passenger + Nginx on a Linux/Unix(Ubuntu) server and deployed a Ruby app. Now my domain looks something like http://example.com when I try to request from a browser.
But I want my domain to default redirect to www every time it is requested from a browser(like http://www.example.com.).



example.conf



 server 
listen 80;
server_name www.example.com example.com;
# return 301 $scheme://www.example.com$request_uri;
# Tell Nginx and Passenger where your app's 'public' directory is
root /var/www/example/public;

# Turn on Passenger
passenger_enabled on;
passenger_spawn_method direct;
passenger_min_instances 1;
#passenger_pool_idle_time 0;
rails_env development;
passenger_ruby /usr/local/rvm/gems/ruby-2.3.3/wrappers/ruby;
passenger_sticky_sessions on;





uncommenting the line




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




is throwing the error



www.example.com redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS


I am forced to manually type www.example.com in the browser. Instead, how can I redirect to www by default?



Any Help is highly appreciated. Thanks in advance!










share|improve this question




























    1















    I brought a domain in godady and I have Installed Passenger + Nginx on a Linux/Unix(Ubuntu) server and deployed a Ruby app. Now my domain looks something like http://example.com when I try to request from a browser.
    But I want my domain to default redirect to www every time it is requested from a browser(like http://www.example.com.).



    example.conf



     server 
    listen 80;
    server_name www.example.com example.com;
    # return 301 $scheme://www.example.com$request_uri;
    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/example/public;

    # Turn on Passenger
    passenger_enabled on;
    passenger_spawn_method direct;
    passenger_min_instances 1;
    #passenger_pool_idle_time 0;
    rails_env development;
    passenger_ruby /usr/local/rvm/gems/ruby-2.3.3/wrappers/ruby;
    passenger_sticky_sessions on;





    uncommenting the line




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




    is throwing the error



    www.example.com redirected you too many times.
    Try clearing your cookies.
    ERR_TOO_MANY_REDIRECTS


    I am forced to manually type www.example.com in the browser. Instead, how can I redirect to www by default?



    Any Help is highly appreciated. Thanks in advance!










    share|improve this question
























      1












      1








      1








      I brought a domain in godady and I have Installed Passenger + Nginx on a Linux/Unix(Ubuntu) server and deployed a Ruby app. Now my domain looks something like http://example.com when I try to request from a browser.
      But I want my domain to default redirect to www every time it is requested from a browser(like http://www.example.com.).



      example.conf



       server 
      listen 80;
      server_name www.example.com example.com;
      # return 301 $scheme://www.example.com$request_uri;
      # Tell Nginx and Passenger where your app's 'public' directory is
      root /var/www/example/public;

      # Turn on Passenger
      passenger_enabled on;
      passenger_spawn_method direct;
      passenger_min_instances 1;
      #passenger_pool_idle_time 0;
      rails_env development;
      passenger_ruby /usr/local/rvm/gems/ruby-2.3.3/wrappers/ruby;
      passenger_sticky_sessions on;





      uncommenting the line




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




      is throwing the error



      www.example.com redirected you too many times.
      Try clearing your cookies.
      ERR_TOO_MANY_REDIRECTS


      I am forced to manually type www.example.com in the browser. Instead, how can I redirect to www by default?



      Any Help is highly appreciated. Thanks in advance!










      share|improve this question














      I brought a domain in godady and I have Installed Passenger + Nginx on a Linux/Unix(Ubuntu) server and deployed a Ruby app. Now my domain looks something like http://example.com when I try to request from a browser.
      But I want my domain to default redirect to www every time it is requested from a browser(like http://www.example.com.).



      example.conf



       server 
      listen 80;
      server_name www.example.com example.com;
      # return 301 $scheme://www.example.com$request_uri;
      # Tell Nginx and Passenger where your app's 'public' directory is
      root /var/www/example/public;

      # Turn on Passenger
      passenger_enabled on;
      passenger_spawn_method direct;
      passenger_min_instances 1;
      #passenger_pool_idle_time 0;
      rails_env development;
      passenger_ruby /usr/local/rvm/gems/ruby-2.3.3/wrappers/ruby;
      passenger_sticky_sessions on;





      uncommenting the line




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




      is throwing the error



      www.example.com redirected you too many times.
      Try clearing your cookies.
      ERR_TOO_MANY_REDIRECTS


      I am forced to manually type www.example.com in the browser. Instead, how can I redirect to www by default?



      Any Help is highly appreciated. Thanks in advance!







      server dns nginx ruby






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 4 at 5:36









      current_usercurrent_user

      1126




      1126




















          2 Answers
          2






          active

          oldest

          votes


















          2














          You basically need two server configuration chunks:



          • One for www; to be redirected to base domain only

          • One for the base domain

          Here is an example to get you started:



          server 
          listen 80;
          server_name www.example.com;

          location /
          # Redirection happens here
          return 301 http://example.com$request_uri;




          server
          listen 80 default_server;
          server_name example.com;

          # Other conf directives go here






          share|improve this answer






























            0














            The easiest way is probably to make use of NGINX if statements. Simply put the following in your virtual host configuration:



            if ($host = "example.com") 
            return 301 $scheme://www.$host$request_uri;



            and be sure to replace "example.com" with your domain name.



            While this is the simplest way I've found, it's not the best. The recommended route is to create a separate server block for non-www, and put the return statement there.






            share|improve this answer




















            • 1





              if does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.

              – Thomas Ward
              2 days ago






            • 1





              According to If Is Evil, return and rewrite can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.

              – NerdOfLinux
              2 days ago











            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "89"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1131082%2fredirect-to-www-prefix-domain-using-nginx%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            You basically need two server configuration chunks:



            • One for www; to be redirected to base domain only

            • One for the base domain

            Here is an example to get you started:



            server 
            listen 80;
            server_name www.example.com;

            location /
            # Redirection happens here
            return 301 http://example.com$request_uri;




            server
            listen 80 default_server;
            server_name example.com;

            # Other conf directives go here






            share|improve this answer



























              2














              You basically need two server configuration chunks:



              • One for www; to be redirected to base domain only

              • One for the base domain

              Here is an example to get you started:



              server 
              listen 80;
              server_name www.example.com;

              location /
              # Redirection happens here
              return 301 http://example.com$request_uri;




              server
              listen 80 default_server;
              server_name example.com;

              # Other conf directives go here






              share|improve this answer

























                2












                2








                2







                You basically need two server configuration chunks:



                • One for www; to be redirected to base domain only

                • One for the base domain

                Here is an example to get you started:



                server 
                listen 80;
                server_name www.example.com;

                location /
                # Redirection happens here
                return 301 http://example.com$request_uri;




                server
                listen 80 default_server;
                server_name example.com;

                # Other conf directives go here






                share|improve this answer













                You basically need two server configuration chunks:



                • One for www; to be redirected to base domain only

                • One for the base domain

                Here is an example to get you started:



                server 
                listen 80;
                server_name www.example.com;

                location /
                # Redirection happens here
                return 301 http://example.com$request_uri;




                server
                listen 80 default_server;
                server_name example.com;

                # Other conf directives go here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 4 at 5:53









                heemaylheemayl

                68.2k11143215




                68.2k11143215























                    0














                    The easiest way is probably to make use of NGINX if statements. Simply put the following in your virtual host configuration:



                    if ($host = "example.com") 
                    return 301 $scheme://www.$host$request_uri;



                    and be sure to replace "example.com" with your domain name.



                    While this is the simplest way I've found, it's not the best. The recommended route is to create a separate server block for non-www, and put the return statement there.






                    share|improve this answer




















                    • 1





                      if does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.

                      – Thomas Ward
                      2 days ago






                    • 1





                      According to If Is Evil, return and rewrite can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.

                      – NerdOfLinux
                      2 days ago















                    0














                    The easiest way is probably to make use of NGINX if statements. Simply put the following in your virtual host configuration:



                    if ($host = "example.com") 
                    return 301 $scheme://www.$host$request_uri;



                    and be sure to replace "example.com" with your domain name.



                    While this is the simplest way I've found, it's not the best. The recommended route is to create a separate server block for non-www, and put the return statement there.






                    share|improve this answer




















                    • 1





                      if does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.

                      – Thomas Ward
                      2 days ago






                    • 1





                      According to If Is Evil, return and rewrite can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.

                      – NerdOfLinux
                      2 days ago













                    0












                    0








                    0







                    The easiest way is probably to make use of NGINX if statements. Simply put the following in your virtual host configuration:



                    if ($host = "example.com") 
                    return 301 $scheme://www.$host$request_uri;



                    and be sure to replace "example.com" with your domain name.



                    While this is the simplest way I've found, it's not the best. The recommended route is to create a separate server block for non-www, and put the return statement there.






                    share|improve this answer















                    The easiest way is probably to make use of NGINX if statements. Simply put the following in your virtual host configuration:



                    if ($host = "example.com") 
                    return 301 $scheme://www.$host$request_uri;



                    and be sure to replace "example.com" with your domain name.



                    While this is the simplest way I've found, it's not the best. The recommended route is to create a separate server block for non-www, and put the return statement there.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 2 days ago

























                    answered 2 days ago









                    NerdOfLinuxNerdOfLinux

                    1,72611041




                    1,72611041







                    • 1





                      if does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.

                      – Thomas Ward
                      2 days ago






                    • 1





                      According to If Is Evil, return and rewrite can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.

                      – NerdOfLinux
                      2 days ago












                    • 1





                      if does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.

                      – Thomas Ward
                      2 days ago






                    • 1





                      According to If Is Evil, return and rewrite can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.

                      – NerdOfLinux
                      2 days ago







                    1




                    1





                    if does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.

                    – Thomas Ward
                    2 days ago





                    if does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.

                    – Thomas Ward
                    2 days ago




                    1




                    1





                    According to If Is Evil, return and rewrite can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.

                    – NerdOfLinux
                    2 days ago





                    According to If Is Evil, return and rewrite can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.

                    – NerdOfLinux
                    2 days ago

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Ask Ubuntu!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1131082%2fredirect-to-www-prefix-domain-using-nginx%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Distance measures on a map of a game The 2019 Stack Overflow Developer Survey Results Are Inmin distance in a graphShortest distance path on contour plotHow to plot a tilted map?Finding points outside of a diskDelaunay link distanceAnnulus from GeoDisks: drawing a ring on a mapNegative Correlation DistanceFind distance along a path (GPS coordinates)Finding position at given distance in a GeoPathMathematics behind distance estimation using camera

                    How to get a smooth, uniform ParametricPlot of a 2D Region?How to plot a complicated Region?How to exclude a region from ParametricPlotHow discretize a region placing vertices on a specific non-uniform gridHow to transform a Plot or a ParametricPlot into a RegionHow can I get a smooth plot of a bounded region?Smooth ParametricPlot3D with RegionFunction?Smooth border of a region ParametricPlotSmooth region boundarySmooth region plot from list of pointsGet minimum y of a certain x in a region

                    Genealogie vun de Merowenger Vum Merowech bis zum Chilperich I. | Navigatiounsmenü