How to redirect the url from nested site in pencilblue?

65 Views Asked by At

I want to 301 redirect the URLs from previous site that are nested, as pencilblue doesn’t support them,

e.g. a/b to page/b

For this I have been experimenting in include/http/request_handler.js but facing some issues.

Call never comes inside RequestHandler.prototype.handleRequest or even RequestHandler.prototype.onSessionRetrieved (seems these methods are not being called from anywhere)

Therefore I placed the code in RequestHandler and after confirming that req is not for public resource or api, I create a new url and execute

return this.doRedirect(newUrl, 301)

This actually works but at the same time I receive

Can’t render headers after they are sent error

#1075 has not helped me much as I’m not sure which specific controller I should modify. I need to catch the req as early as possible and see if it’s a page then redirect to page prefixed url.

Thanks in advance.

1

There are 1 best solutions below

0
On

There are couple of ways to do redirects. You can do them from a controller or from middleware. You are correct in that, some of the functions in the request handler are not called. These are deprecated despite the fact pencilblue team didn't mark them as such. They replaced a good deal of the request handler functionality with /include/http/router.js and include/http/middleware/index.js. Plugins can register their own middleware to hijack the request pipeline.

See Advanced Routing on wiki for more info about creating your own middleware.

Using the routing framework your plugin would be able to register middleware that would be able to inspect the request and then redirect based on your specific criteria. The Router will be accessible from req.router and from there you could call req.router.redirect (Source).

Reference: #1224