redirect and canonical tag
This commit is contained in:
parent
26064d749c
commit
b836d38e70
4 changed files with 50 additions and 0 deletions
29
hub/middleware.py
Normal file
29
hub/middleware.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
from django.conf import settings
|
||||||
|
from django.http import HttpResponseRedirect
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
|
||||||
|
class PrimaryDomainRedirectMiddleware:
|
||||||
|
def __init__(self, get_response):
|
||||||
|
self.get_response = get_response
|
||||||
|
# Parse the primary hostname from WEBSITE_URL
|
||||||
|
self.primary_host = urlparse(settings.WEBSITE_URL).netloc
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
|
# Skip redirects in DEBUG mode
|
||||||
|
if settings.DEBUG:
|
||||||
|
return self.get_response(request)
|
||||||
|
|
||||||
|
# Check if the host is different from the primary host
|
||||||
|
if request.get_host() != self.primary_host:
|
||||||
|
# Build the redirect URL
|
||||||
|
scheme = "https" # Always use HTTPS for redirects
|
||||||
|
path = request.get_full_path() # Includes query string
|
||||||
|
redirect_url = f"{scheme}://{self.primary_host}{path}"
|
||||||
|
|
||||||
|
# Use 301 permanent redirect
|
||||||
|
response = HttpResponseRedirect(redirect_url)
|
||||||
|
response.status_code = 301
|
||||||
|
return response
|
||||||
|
|
||||||
|
return self.get_response(request)
|
|
@ -1,10 +1,12 @@
|
||||||
{% load static %}
|
{% load static %}
|
||||||
|
{% load canonical_tags %}
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Servala - {% block title %}{% endblock %}</title>
|
<title>Servala - {% block title %}{% endblock %}</title>
|
||||||
|
<link rel="canonical" href="{% canonical_url %}" />
|
||||||
<link rel="icon" type="image/x-icon" href="{% static "img/favicon.ico" %}">
|
<link rel="icon" type="image/x-icon" href="{% static "img/favicon.ico" %}">
|
||||||
|
|
||||||
<link rel="stylesheet" href='{% static "css/bootstrap-icons.min.css" %}'>
|
<link rel="stylesheet" href='{% static "css/bootstrap-icons.min.css" %}'>
|
||||||
|
|
18
hub/services/templatetags/canonical_tags.py
Normal file
18
hub/services/templatetags/canonical_tags.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
from django import template
|
||||||
|
from django.conf import settings
|
||||||
|
from urllib.parse import urljoin
|
||||||
|
|
||||||
|
register = template.Library()
|
||||||
|
|
||||||
|
|
||||||
|
@register.simple_tag(takes_context=True)
|
||||||
|
def canonical_url(context):
|
||||||
|
"""
|
||||||
|
Returns the canonical URL for the current page
|
||||||
|
"""
|
||||||
|
request = context["request"]
|
||||||
|
path = request.path
|
||||||
|
|
||||||
|
# Construct the canonical URL using the WEBSITE_URL from settings
|
||||||
|
canonical = urljoin(settings.WEBSITE_URL, path)
|
||||||
|
return canonical
|
|
@ -78,6 +78,7 @@ if DEBUG:
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
"django.middleware.security.SecurityMiddleware",
|
"django.middleware.security.SecurityMiddleware",
|
||||||
|
"hub.middleware.PrimaryDomainRedirectMiddleware",
|
||||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||||
"django.middleware.common.CommonMiddleware",
|
"django.middleware.common.CommonMiddleware",
|
||||||
"django.middleware.csrf.CsrfViewMiddleware",
|
"django.middleware.csrf.CsrfViewMiddleware",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue