diff --git a/IPython/html/services/kernels/handlers.py b/IPython/html/services/kernels/handlers.py
index c5bdec1..c104207 100644
--- a/IPython/html/services/kernels/handlers.py
+++ b/IPython/html/services/kernels/handlers.py
@@ -22,6 +22,7 @@ from tornado import web
from zmq.utils import jsonapi
from IPython.utils.jsonutil import date_default
+from IPython.html.utils import url_path_join
from ...base.handlers import IPythonHandler, json_errors
from ...base.zmqhandlers import AuthenticatedZMQStreamHandler
@@ -45,7 +46,8 @@ class MainKernelHandler(IPythonHandler):
km = self.kernel_manager
kernel_id = km.start_kernel()
model = km.kernel_model(kernel_id, self.ws_url)
- self.set_header('Location', '{0}api/kernels/{1}'.format(self.base_kernel_url, kernel_id))
+ location = url_path_join(self.base_kernel_url, 'api', 'kernels', kernel_id)
+ self.set_header('Location', location)
self.set_status(201)
self.finish(jsonapi.dumps(model))
diff --git a/IPython/html/services/sessions/handlers.py b/IPython/html/services/sessions/handlers.py
index b4bfb9b..cb8a084 100644
--- a/IPython/html/services/sessions/handlers.py
+++ b/IPython/html/services/sessions/handlers.py
@@ -19,8 +19,10 @@ Authors:
import json
from tornado import web
-from IPython.utils.jsonutil import date_default
+
from ...base.handlers import IPythonHandler, json_errors
+from IPython.utils.jsonutil import date_default
+from IPython.html.utils import url_path_join
#-----------------------------------------------------------------------------
# Session web service handlers
@@ -62,7 +64,8 @@ class SessionRootHandler(IPythonHandler):
else:
kernel_id = km.start_kernel(cwd=nbm.notebook_dir)
model = sm.create_session(name=name, path=path, kernel_id=kernel_id, ws_url=self.ws_url)
- self.set_header('Location', '{0}/api/sessions/{1}'.format(self.base_project_url, model['id']))
+ location = url_path_join(self.base_kernel_url, 'api', 'sessions', model['id'])
+ self.set_header('Location', location)
self.set_status(201)
self.finish(json.dumps(model, default=date_default))
diff --git a/IPython/html/services/sessions/tests/test_sessions_api.py b/IPython/html/services/sessions/tests/test_sessions_api.py
index e36cdd7..363d68b 100644
--- a/IPython/html/services/sessions/tests/test_sessions_api.py
+++ b/IPython/html/services/sessions/tests/test_sessions_api.py
@@ -74,6 +74,7 @@ class SessionAPITest(NotebookTestBase):
self.assertIn('id', newsession)
self.assertEqual(newsession['notebook']['name'], 'nb1.ipynb')
self.assertEqual(newsession['notebook']['path'], 'foo')
+ self.assertEqual(resp.headers['Location'], '/api/sessions/{0}'.format(newsession['id']))
sessions = self.sess_api.list().json()
self.assertEqual(sessions, [newsession])