Contents
collective.scriptedredirect
allows you to write HTTP 302 Moved Temporary and HTTP 301 Moved Permanently
redirects for your Plone CMS site in Python code.
- The redirect logic is front-end web server independent: no need to touch variouos configuration files of Apache, Varnish or Nginx)
- Python allows to write more complex logic for redirects easier - no regular expressions!
- Python scripts in Plone have access to more complete state information: user logged in status, permissions, etc.
Plone 5.0 and 5.1 versions supported.
Add add-on in buildout.cfg:
eggs = ... collective.scriptedredirect
Run buildout.
Install Scripted redirects in Python in Site Setup > Add-ons.
Edit redirect_handler in Zope Management Interface in your site root.
In the case of accident use ?no_redirect
HTTP query parameter to override
the redirecter and fix your site.
You can also register a browser view called redirect_handler
.
In this case, you write the redirect code in addon Python code
and not through the web.
redirect_handler
view is always preferred over redirect_handler
script.
Example Python code in redirector.py
:
class TestingRedirectHandler(object): """ Redirect handler registered as a ``redirect_handler`` Zope 3 <browser:page> """ def __init__(self, context, request): self.context = context self.request = request def __call__(self, url, host, port, path): """ Handle redirects per site. :param path: Path as written in HTTP request (not site virtual path) :return: None if no redirect needed, otherwise a string full HTTP URL to the redirect target :raise: zExceptions.Redirect or other custom redirect exception if needed """ # Simple example: always access site over www. domain prefix if not url.startswith("http://www."): return url.replace("http://", "http://www.") # Don't redirect if we are already using www. prefix return None
Example ZCML:
<browser:page name="redirect_handler" for="Products.CMFCore.interfaces.ISiteRoot" layer="YOUR_ADDON_LAYER" class=".redirector.TestRedirectHandler" permission="zope.Public" />
collective.scriptedredirect
hooks itself to Zope's pre-traversal hook and is
triggered before the request traverses into your Plone site in Zope application server.