From: James Moger Date: Fri, 25 Oct 2013 16:18:23 +0000 (-0400) Subject: Set Link: ; rel="canonical" http header for SEO (issue-304) X-Git-Tag: v1.4.0~218 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3079105705d76ff0fbfcc755825aa75b0b6ef01a;p=gitblit.git Set Link: ; rel="canonical" http header for SEO (issue-304) Change-Id: Ie023b5fc58bf988e9064700918a79718aaa6c94b --- diff --git a/releases.moxie b/releases.moxie index e9ec642d..403d34fe 100644 --- a/releases.moxie +++ b/releases.moxie @@ -30,6 +30,7 @@ r20: { - Added branch graph image servlet based on EGit's branch graph renderer (issue-194) - Added option to render Markdown commit messages (issue-203) - Added setting to control creating a repository as --shared on Unix servers (issue-263) + - Set Link: ; rel="canonical" http header for SEO (issue-304) - Added raw links to the commit, commitdiff, and compare pages (issue-319) - Support intradocument linking in Markdown content using [[WikiLinks]] syntax (issue-324) - Added setting to globally disable anonymous pushes in the receive pack diff --git a/src/main/java/com/gitblit/wicket/pages/BasePage.java b/src/main/java/com/gitblit/wicket/pages/BasePage.java index cb9de9fa..f8816831 100644 --- a/src/main/java/com/gitblit/wicket/pages/BasePage.java +++ b/src/main/java/com/gitblit/wicket/pages/BasePage.java @@ -88,6 +88,16 @@ public abstract class BasePage extends SessionPage { } } + protected String getCanonicalUrl() { + return getCanonicalUrl(getClass(), getPageParameters()); + } + + protected String getCanonicalUrl(Class clazz, PageParameters params) { + String relativeUrl = urlFor(clazz, params).toString(); + String canonicalUrl = RequestUtils.toAbsolutePath(relativeUrl); + return canonicalUrl; + } + protected String getLanguageCode() { return GitBlitWebSession.get().getLocale().getLanguage(); } @@ -129,6 +139,9 @@ public abstract class BasePage extends SessionPage { @Override protected void setHeaders(WebResponse response) { + // set canonical link as http header for SEO (issue-304) + // https://support.google.com/webmasters/answer/139394?hl=en + response.setHeader("Link" ,MessageFormat.format("<{0}>; rel=\"canonical\"", getCanonicalUrl())); int expires = GitBlit.getInteger(Keys.web.pageCacheExpires, 0); if (expires > 0) { // pages are personalized for the authenticated user so they must be diff --git a/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java b/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java index 508b8654..68aa9a7a 100644 --- a/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java +++ b/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java @@ -39,7 +39,6 @@ import org.apache.wicket.markup.html.link.ExternalLink; import org.apache.wicket.markup.html.panel.Fragment; import org.apache.wicket.model.IModel; import org.apache.wicket.model.Model; -import org.apache.wicket.protocol.http.RequestUtils; import org.apache.wicket.request.target.basic.RedirectRequestTarget; import org.eclipse.jgit.diff.DiffEntry.ChangeType; import org.eclipse.jgit.lib.PersonIdent; @@ -721,9 +720,7 @@ public abstract class RepositoryPage extends RootPage { String searchString = searchBoxModel.getObject(); if (StringUtils.isEmpty(searchString)) { // redirect to self to avoid wicket page update bug - PageParameters params = RepositoryPage.this.getPageParameters(); - String relativeUrl = urlFor(RepositoryPage.this.getClass(), params).toString(); - String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl); + String absoluteUrl = getCanonicalUrl(); getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl)); return; } @@ -745,8 +742,7 @@ public abstract class RepositoryPage extends RootPage { // use an absolute url to workaround Wicket-Tomcat problems with // mounted url parameters (issue-111) PageParameters params = WicketUtils.newSearchParameter(repositoryName, null, searchString, searchType); - String relativeUrl = urlFor(searchPageClass, params).toString(); - String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl); + String absoluteUrl = getCanonicalUrl(searchPageClass, params); getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl)); } }