Repository: getnikola/nikola

Issue:
Nikola auto should redirect directories to add trailing slash
since #1430 is closed, please let me create another one. 
also not sure if it is related to docs or to rendering being erroneous.

I'm using Nikola v8.0.0b3, python 3.6.5 on ubuntu 18.04

I'm trying to create a hierarchical navigation menu by creating an entry like this in conf.py
```
    (
        (
            ('/fruits/apple', 'Apple'),
            ('/fruits/orange', 'Orange'),
        ),
        'Fruits'
    )
```
the site looks fine from index page, the links look like `127.0.0.1:8000/fruits/apple`,
but when I navigate to that page and check navbar again, the links become like `127.0.0.1:8000/apple`, the first part mysteriously disappears.

if I crate links like  `('fruits/apple', 'Apple')`, then from the subpage it becoms `/fruits/fruits/apple` :-\

Nikola auto should redirect directories to add trailing slash
since #1430 is closed, please let me create another one. 
also not sure if it is related to docs or to rendering being erroneous.

I'm using Nikola v8.0.0b3, python 3.6.5 on ubuntu 18.04

I'm trying to create a hierarchical navigation menu by creating an entry like this in conf.py
```
    (
        (
            ('/fruits/apple', 'Apple'),
            ('/fruits/orange', 'Orange'),
        ),
        'Fruits'
    )
```
the site looks fine from index page, the links look like `127.0.0.1:8000/fruits/apple`,
but when I navigate to that page and check navbar again, the links become like `127.0.0.1:8000/apple`, the first part mysteriously disappears.

if I crate links like  `('fruits/apple', 'Apple')`, then from the subpage it becoms `/fruits/fruits/apple` :-\



Discussion:
Cannot reproduce. What theme are you using and how do you test it? The link is supposed to be `href="apple"` on that page, but it still works, since we use relative links for everything by default.
I am using bootstrap4 theme
Interestingly I have just noticed that when I deploy on github pages - links work ok, but when I serve locally with nikola auto - the link from the nav nested page is broken! :-\
on the subpage (/fruits/apple) the link is rendered as
`<a href="../apple" class="dropdown-item">Apple</a>`
here's how it looks:
![index](https://user-images.githubusercontent.com/1635907/44246606-fac69300-a219-11e8-9679-4189522205c2.png)
![apple](https://user-images.githubusercontent.com/1635907/44246611-fef2b080-a219-11e8-96cc-19e93d4aefb7.png)


and here's the source:
https://github.com/crest-deep/crest-deep.github.io/tree/src
just checked other themes - same problem with bootblog4 and base
I see... from /fruits/apple "../apple" interprets last piece as a file name, not a folder... 
adding / to the end helps, so does `URL_TYPE = 'full_path'`
now not sure if it is a bug or a feature :)

1. We recommend putting a forward slash / at the end of links in the navbar
2. Changing URL_TYPE should not be necessary
3. This appears to be a bug in nikola auto, it should redirect from /foo to
/foo/

-- 
Chris Warrick <https://chriswarrick.com/>

On Fri, Aug 17, 2018, 14:14 Aleksandr Drozd <notifications@github.com>
wrote:

> Closed #3140 <https://github.com/getnikola/nikola/issues/3140>.
>
> —
> You are receiving this because you were assigned.
> Reply to this email directly, view it on GitHub
> <https://github.com/getnikola/nikola/issues/3140#event-1794530751>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AAT-m5dckYQlK1IxWBzXL0OraobvHuxlks5uRrO-gaJpZM4V_dZS>
> .
>

I think we should reopen it, and yes, the real fix is the redirect @Kwpolska mentions.
I’m taking over, PR coming in 5 minutes.
Cannot reproduce. What theme are you using and how do you test it? The link is supposed to be `href="apple"` on that page, but it still works, since we use relative links for everything by default.
I am using bootstrap4 theme
Interestingly I have just noticed that when I deploy on github pages - links work ok, but when I serve locally with nikola auto - the link from the nav nested page is broken! :-\
on the subpage (/fruits/apple) the link is rendered as
`<a href="../apple" class="dropdown-item">Apple</a>`
here's how it looks:
![index](https://user-images.githubusercontent.com/1635907/44246606-fac69300-a219-11e8-9679-4189522205c2.png)
![apple](https://user-images.githubusercontent.com/1635907/44246611-fef2b080-a219-11e8-96cc-19e93d4aefb7.png)


and here's the source:
https://github.com/crest-deep/crest-deep.github.io/tree/src
just checked other themes - same problem with bootblog4 and base
I see... from /fruits/apple "../apple" interprets last piece as a file name, not a folder... 
adding / to the end helps, so does `URL_TYPE = 'full_path'`
now not sure if it is a bug or a feature :)

1. We recommend putting a forward slash / at the end of links in the navbar
2. Changing URL_TYPE should not be necessary
3. This appears to be a bug in nikola auto, it should redirect from /foo to
/foo/

-- 
Chris Warrick <https://chriswarrick.com/>

On Fri, Aug 17, 2018, 14:14 Aleksandr Drozd <notifications@github.com>
wrote:

> Closed #3140 <https://github.com/getnikola/nikola/issues/3140>.
>
> —
> You are receiving this because you were assigned.
> Reply to this email directly, view it on GitHub
> <https://github.com/getnikola/nikola/issues/3140#event-1794530751>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AAT-m5dckYQlK1IxWBzXL0OraobvHuxlks5uRrO-gaJpZM4V_dZS>
> .
>

I think we should reopen it, and yes, the real fix is the redirect @Kwpolska mentions.
I’m taking over, PR coming in 5 minutes.

Resolving patch:
diff --git a/nikola/plugins/command/auto/__init__.py b/nikola/plugins/command/auto/__init__.py
--- a/nikola/plugins/command/auto/__init__.py
+++ b/nikola/plugins/command/auto/__init__.py
@@ -38,7 +38,7 @@
     import aiohttp
     from aiohttp import web
     from aiohttp.web_urldispatcher import StaticResource
-    from aiohttp.web_exceptions import HTTPNotFound, HTTPForbidden
+    from aiohttp.web_exceptions import HTTPNotFound, HTTPForbidden, HTTPMovedPermanently
     from aiohttp.web_response import Response
     from aiohttp.web_fileresponse import FileResponse
 except ImportError:
@@ -256,7 +256,7 @@ def _execute(self, options, args):
             if self.dns_sd:
                 self.dns_sd.Reset()
             srv.close()
-            self.rebuild_queue.put((None, None))
+            loop.run_until_complete(self.rebuild_queue.put((None, None)))
             loop.run_until_complete(srv.wait_closed())
             loop.run_until_complete(webapp.shutdown())
             loop.run_until_complete(handler.shutdown(5.0))
@@ -432,7 +432,11 @@ def handle_file(self, request, filename, from_index=None):
             if filename.endswith('/') or not filename:
                 ret = yield from self.handle_file(request, filename + 'index.html', from_index=filename)
             else:
-                ret = yield from self.handle_file(request, filename + '/index.html', from_index=filename)
+                # Redirect and add trailing slash so relative links work (Issue #3140)
+                new_url = request.rel_url.path + '/'
+                if request.rel_url.query_string:
+                    new_url += '?' + request.rel_url.query_string
+                raise HTTPMovedPermanently(new_url)
         elif filepath.is_file():
             ct, encoding = mimetypes.guess_type(str(filepath))
             encoding = encoding or 'utf-8'


Test patch: