Skip to content

Commit

Permalink
OnlyLang;: Permanently redirect / to first language
Browse files Browse the repository at this point in the history
For the OnlyLang ingress builder, use a permanent redirect (301) instead of the default temporary one (302), to improve SEO performance.

Closes #116
  • Loading branch information
Stefan Bethke committed Jan 4, 2024
1 parent 61d150d commit 25847dc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,20 @@ public interface IngressBuilder {
IngressBuilder redirect(String uri);

/**
* Sets the response timeout for this ingress.
* Redirect the client to the new relative or absolute URL, using status code {@code code}.
*
* @param seconds timeout
* @param uri the target URI
* @param code the HTTP status code for the redirect, for example, 301
* @return the builder
*/
IngressBuilder redirect(String uri, int code);

/**
* Sets the response timeout for this ingress.
*
* @param seconds timeout
* @return the builder
*/
IngressBuilder responseTimeout(int seconds);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public IngressBuilder pathExact(String path, String service) {

@Override
public IngressBuilder pathPattern(String path, String service) {
annotations.put("nginx.ingress.kubernetes.io/use-regex", "true");
paths.add(new Path(path, PathType.PATTERN, service));
return this;
}
Expand All @@ -98,6 +99,14 @@ public IngressBuilder redirect(String uri) {
return this;
}

@Override
public IngressBuilder redirect(String uri, int code) {
annotations.put("nginx.ingress.kubernetes.io/permanent-redirect", uri);
if (code != 301)
annotations.put("nginx.ingress.kubernetes.io/permanent-redirect-code", String.valueOf(code));
return this;
}

@Override
public IngressBuilder rewrite(String pattern) {
annotations.put("nginx.ingress.kubernetes.io/rewrite-target", pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.tsystemsmms.cmcc.cmccoperator.targetstate.TargetState;
import io.fabric8.kubernetes.api.model.HasMetadata;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;

import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -38,7 +39,7 @@
* <p>
* The resulting rewrite rules build these mappings:
* <ul>
* <li>https://corporate.example.de/ redirects to /de/</li>
* <li>https://corporate.example.de/ redirects to /de</li>
* <li>https://corporate.example.de/de maps to corporate-de-de</li>
* <li>https://corporate.example.ca/ redirects to /en</li>
* <li>https://corporate.example.ca/en maps to corporate-en-ca</li>
Expand Down Expand Up @@ -81,7 +82,9 @@ public Collection<? extends HasMetadata> buildLiveResources(SiteMapping siteMapp
ingresses.addAll(ingressBuilderFactory.builder(targetState, liveName(site, "home", suffix), fqdn, tls)
.responseTimeout(responseTimeout)
.uploadSize(uploadSize)
.pathExact("/", serviceName).redirect("/" + getLanguage(siteMapping.getPrimarySegment())).build());
.pathPattern("/", serviceName)
.redirect("$scheme://" + fqdn + "/" + getLanguage(siteMapping.getPrimarySegment()), HttpStatus.MOVED_PERMANENTLY.value())
.build());
ingresses.addAll(ingressBuilderFactory.builder(targetState, liveName(site, "blueprint", suffix), fqdn, tls)
.responseTimeout(responseTimeout)
.uploadSize(uploadSize)
Expand All @@ -97,7 +100,7 @@ public Collection<? extends HasMetadata> buildLiveResources(SiteMapping siteMapp
ingresses.addAll(ingressBuilderFactory.builder(targetState, liveName(site, "default", suffix), fqdn, tls)
.responseTimeout(responseTimeout)
.uploadSize(uploadSize)
.pathPattern("/(.*)", serviceName).rewrite("/blueprint/servlet/" + siteMapping.getPrimarySegment() + "/$1").build());
.pathPattern("/(.+)", serviceName).rewrite("/blueprint/servlet/" + siteMapping.getPrimarySegment() + "/$1").build());
ingresses.addAll(ingressBuilderFactory.builder(targetState, liveName(site, "seo", suffix), fqdn, tls)
.responseTimeout(responseTimeout)
.uploadSize(uploadSize)
Expand Down

0 comments on commit 25847dc

Please sign in to comment.