-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef9918a
commit 2c4a964
Showing
3 changed files
with
28 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.http import HttpResponsePermanentRedirect | ||
|
||
|
||
class NoWWWRedirectMiddleware: | ||
def __init__(self, get_response=None): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
response = self.process_request(request) | ||
return response or self.get_response(request) | ||
|
||
def process_request(self, request): | ||
host = request.get_host() | ||
if host.startswith("www."): | ||
if request.method == "GET": | ||
no_www = host[4:] | ||
url = request.build_absolute_uri().replace(host, no_www, 1) | ||
return HttpResponsePermanentRedirect(url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters