Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

requestPreprocessor

Jan Max Meyer edited this page Feb 21, 2019 · 1 revision

The request pre-processor can be used to perform rewriting and other, request-related stuff before the normal ViUR function routing will be executed.

Usually, a request pre-processor is put into the project's main file right under the deploy/ folder, and configured using conf["viur.requestPreprocessor"].

def handleRequest(path):
	"""
	This simple request preprocessor can be used to canalize all requests coming
	from several domains and 301 redirects them to a main URL.
	"""
	if "X-AppEngine-TaskName" in server.request.current.get().request.headers:
		return path

	mainUrl = "https://www.your-domain.com"
	url = server.request.current.get().request.url.lower()

	if not url.startswith(mainUrl):
		for proto in ["http://", "https://"]:
			if url.startswith(proto):
				for other in ["your-domain.com", "www.your-domain.com",
								"your-other-domain.com", "www.your-other-domain.com"]:
					if url[len(proto):].startswith(other):
						raise server.errors.Redirect(mainUrl + url[len(proto) + len(other):], status=301)

	return path

conf["viur.requestPreprocessor"] = handleRequest