Redirect all subdirectories to root with nginxIn Nginx, how can I rewrite all http requests to https while maintaining sub-domain?How does try_files work?nginx redirect issue with upstream configurationNginx - redirect all url's to root (not show the HTML page in the URL?)Nginx: Redirect both http and https root to subdirectoryAvoid double redirect NGINXNginx - Redirect all previous subdomain's sub directories to main domain's subdirectoriesCodeIgniter nginx rewrite rules for i8ln URL'sStripping index.html and .html from URLs with nginxHow do I redirect subdomains to the root domain in Nginx on CentOS?

Find the common ancestor between two nodes of a tree

Improve appearance of the table in Latex

What are the pros and cons for the two possible "gear directions" when parking the car on a hill?

Can the pre-order traversal of two different trees be the same even though they are different?

Is the continuity test limit resistance of a multimeter standard?

FD Battery Stations... How Do You Log?

How could empty set be unique if it could be vacuously false

Mathematically modelling RC circuit with a linear input

How did Gollum enter Moria?

How can I prevent a user from copying files on another hard drive?

Dmesg full of I/O errors, smart ok, four disks affected

Non-misogynistic way to say “asshole”?

Where should a runway for a spaceplane be located?

How do internally carried IR missiles acquire a lock?

What are Elsa's reasons for selecting the Holy Grail on behalf of Donovan?

Why is "Congress shall have power to enforce this article by appropriate legislation" necessary?

Cut the gold chain

Is "Busen" just the area between the breasts?

What are the current battlegrounds for people’s “rights” in the UK?

How much steel armor can you wear and still be able to swim?

Extending prime numbers digit by digit while retaining primality

Can you use one creature for both convoke and delve for Hogaak?

Is the specular reflection on a polished gold sphere white or gold in colour?

Greeting with "Ho"



Redirect all subdirectories to root with nginx


In Nginx, how can I rewrite all http requests to https while maintaining sub-domain?How does try_files work?nginx redirect issue with upstream configurationNginx - redirect all url's to root (not show the HTML page in the URL?)Nginx: Redirect both http and https root to subdirectoryAvoid double redirect NGINXNginx - Redirect all previous subdomain's sub directories to main domain's subdirectoriesCodeIgniter nginx rewrite rules for i8ln URL'sStripping index.html and .html from URLs with nginxHow do I redirect subdomains to the root domain in Nginx on CentOS?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm running a web application at my site's root. I would like it so that all subdirectories (even ones that exist) are directed to the root e.g. example.com/anything would redirect to example.com.



Gmail does this - if I'm viewing my inbox at:



https://mail.google.com/mail/#inbox


And I attempt to go to:



https://mail.google.com/mail/#inbox/some-other-place


I am just sent back to https://mail.google.com/mail/#inbox, rather than being presented with an error page.



How can I accomplish this in nginx?



If I just stick return 301... inside the root location block, I end up with a redirect loop. I tried also using try_files like this:



location / 
try_files $uri $uri/ @home;


location @home
return 301 example.com;



This works for non-existent files or directories, however, nginx will still serve subdirectories and files if they do exist.



Thanks.










share|improve this question




























    0















    I'm running a web application at my site's root. I would like it so that all subdirectories (even ones that exist) are directed to the root e.g. example.com/anything would redirect to example.com.



    Gmail does this - if I'm viewing my inbox at:



    https://mail.google.com/mail/#inbox


    And I attempt to go to:



    https://mail.google.com/mail/#inbox/some-other-place


    I am just sent back to https://mail.google.com/mail/#inbox, rather than being presented with an error page.



    How can I accomplish this in nginx?



    If I just stick return 301... inside the root location block, I end up with a redirect loop. I tried also using try_files like this:



    location / 
    try_files $uri $uri/ @home;


    location @home
    return 301 example.com;



    This works for non-existent files or directories, however, nginx will still serve subdirectories and files if they do exist.



    Thanks.










    share|improve this question
























      0












      0








      0








      I'm running a web application at my site's root. I would like it so that all subdirectories (even ones that exist) are directed to the root e.g. example.com/anything would redirect to example.com.



      Gmail does this - if I'm viewing my inbox at:



      https://mail.google.com/mail/#inbox


      And I attempt to go to:



      https://mail.google.com/mail/#inbox/some-other-place


      I am just sent back to https://mail.google.com/mail/#inbox, rather than being presented with an error page.



      How can I accomplish this in nginx?



      If I just stick return 301... inside the root location block, I end up with a redirect loop. I tried also using try_files like this:



      location / 
      try_files $uri $uri/ @home;


      location @home
      return 301 example.com;



      This works for non-existent files or directories, however, nginx will still serve subdirectories and files if they do exist.



      Thanks.










      share|improve this question














      I'm running a web application at my site's root. I would like it so that all subdirectories (even ones that exist) are directed to the root e.g. example.com/anything would redirect to example.com.



      Gmail does this - if I'm viewing my inbox at:



      https://mail.google.com/mail/#inbox


      And I attempt to go to:



      https://mail.google.com/mail/#inbox/some-other-place


      I am just sent back to https://mail.google.com/mail/#inbox, rather than being presented with an error page.



      How can I accomplish this in nginx?



      If I just stick return 301... inside the root location block, I end up with a redirect loop. I tried also using try_files like this:



      location / 
      try_files $uri $uri/ @home;


      location @home
      return 301 example.com;



      This works for non-existent files or directories, however, nginx will still serve subdirectories and files if they do exist.



      Thanks.







      nginx redirect






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 15 '14 at 14:37









      EllisEllis

      291128




      291128




















          1 Answer
          1






          active

          oldest

          votes


















          0














          Anything after the hashtag is not sent to the server by the browser, it is processed by the JS code in the browser.



          If you want to do that with real paths, not with hashtags, you have to make 2 locations:



          location / 
          root ...;
          # no redirect


          location ~ ^/.+$
          try_files



          This will avoid the redirect loop.






          share|improve this answer























          • The problem is, if I put try_files under the second block there, it will match existing files, so they won't get redirected (like my example in my question). ALSO, one thing I forgot to mention is that the app is a php app. I'm using php-fpm so php files need to still be passed to FastCGI (i.e. not redirected to the root).

            – Ellis
            Apr 15 '14 at 15:16











          • Also, putting the return line in the second location block causes a redirect loop. I'm not sure why, because I thought that regex would match one or more characters after the '/'.

            – Ellis
            Apr 15 '14 at 15:27











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "2"
          ;
          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%2fserverfault.com%2fquestions%2f589186%2fredirect-all-subdirectories-to-root-with-nginx%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Anything after the hashtag is not sent to the server by the browser, it is processed by the JS code in the browser.



          If you want to do that with real paths, not with hashtags, you have to make 2 locations:



          location / 
          root ...;
          # no redirect


          location ~ ^/.+$
          try_files



          This will avoid the redirect loop.






          share|improve this answer























          • The problem is, if I put try_files under the second block there, it will match existing files, so they won't get redirected (like my example in my question). ALSO, one thing I forgot to mention is that the app is a php app. I'm using php-fpm so php files need to still be passed to FastCGI (i.e. not redirected to the root).

            – Ellis
            Apr 15 '14 at 15:16











          • Also, putting the return line in the second location block causes a redirect loop. I'm not sure why, because I thought that regex would match one or more characters after the '/'.

            – Ellis
            Apr 15 '14 at 15:27















          0














          Anything after the hashtag is not sent to the server by the browser, it is processed by the JS code in the browser.



          If you want to do that with real paths, not with hashtags, you have to make 2 locations:



          location / 
          root ...;
          # no redirect


          location ~ ^/.+$
          try_files



          This will avoid the redirect loop.






          share|improve this answer























          • The problem is, if I put try_files under the second block there, it will match existing files, so they won't get redirected (like my example in my question). ALSO, one thing I forgot to mention is that the app is a php app. I'm using php-fpm so php files need to still be passed to FastCGI (i.e. not redirected to the root).

            – Ellis
            Apr 15 '14 at 15:16











          • Also, putting the return line in the second location block causes a redirect loop. I'm not sure why, because I thought that regex would match one or more characters after the '/'.

            – Ellis
            Apr 15 '14 at 15:27













          0












          0








          0







          Anything after the hashtag is not sent to the server by the browser, it is processed by the JS code in the browser.



          If you want to do that with real paths, not with hashtags, you have to make 2 locations:



          location / 
          root ...;
          # no redirect


          location ~ ^/.+$
          try_files



          This will avoid the redirect loop.






          share|improve this answer













          Anything after the hashtag is not sent to the server by the browser, it is processed by the JS code in the browser.



          If you want to do that with real paths, not with hashtags, you have to make 2 locations:



          location / 
          root ...;
          # no redirect


          location ~ ^/.+$
          try_files



          This will avoid the redirect loop.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 15 '14 at 14:43









          Florin AsăvoaieFlorin Asăvoaie

          6,3541733




          6,3541733












          • The problem is, if I put try_files under the second block there, it will match existing files, so they won't get redirected (like my example in my question). ALSO, one thing I forgot to mention is that the app is a php app. I'm using php-fpm so php files need to still be passed to FastCGI (i.e. not redirected to the root).

            – Ellis
            Apr 15 '14 at 15:16











          • Also, putting the return line in the second location block causes a redirect loop. I'm not sure why, because I thought that regex would match one or more characters after the '/'.

            – Ellis
            Apr 15 '14 at 15:27

















          • The problem is, if I put try_files under the second block there, it will match existing files, so they won't get redirected (like my example in my question). ALSO, one thing I forgot to mention is that the app is a php app. I'm using php-fpm so php files need to still be passed to FastCGI (i.e. not redirected to the root).

            – Ellis
            Apr 15 '14 at 15:16











          • Also, putting the return line in the second location block causes a redirect loop. I'm not sure why, because I thought that regex would match one or more characters after the '/'.

            – Ellis
            Apr 15 '14 at 15:27
















          The problem is, if I put try_files under the second block there, it will match existing files, so they won't get redirected (like my example in my question). ALSO, one thing I forgot to mention is that the app is a php app. I'm using php-fpm so php files need to still be passed to FastCGI (i.e. not redirected to the root).

          – Ellis
          Apr 15 '14 at 15:16





          The problem is, if I put try_files under the second block there, it will match existing files, so they won't get redirected (like my example in my question). ALSO, one thing I forgot to mention is that the app is a php app. I'm using php-fpm so php files need to still be passed to FastCGI (i.e. not redirected to the root).

          – Ellis
          Apr 15 '14 at 15:16













          Also, putting the return line in the second location block causes a redirect loop. I'm not sure why, because I thought that regex would match one or more characters after the '/'.

          – Ellis
          Apr 15 '14 at 15:27





          Also, putting the return line in the second location block causes a redirect loop. I'm not sure why, because I thought that regex would match one or more characters after the '/'.

          – Ellis
          Apr 15 '14 at 15:27

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Server Fault!


          • 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%2fserverfault.com%2fquestions%2f589186%2fredirect-all-subdirectories-to-root-with-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

          How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

          What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

          Why did Thanos need his ship to help him in the battle scene?Which actor plays Thanos in the Avengers mid-credits scene?Are there economic implications portrayed in comics where the buildings and cities are ruined almost daily?Old X-Men comic where team travels to alien world with a ring-like sun that needs recharging?Why does Ego need help sleeping?Is there an objective answer to who “the strongest Avenger” is?How did Banner get unstuck?Why did Thanos get hit?How did Thanos (or anyone) know the Infinity Stones would give him this power?Did Thanos leave Eitri alive for his after-sales service?In Avengers 1, why does Thanos need Loki?