summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2013-10-25 12:18:23 -0400
committerJames Moger <james.moger@gitblit.com>2013-10-25 12:18:23 -0400
commit3079105705d76ff0fbfcc755825aa75b0b6ef01a (patch)
tree90a9e7a75bb649c1fddda72f0b31559e7ca89518
parent96a6dae0c86e505e129ce4af33886f4dda69d169 (diff)
downloadgitblit-3079105705d76ff0fbfcc755825aa75b0b6ef01a.tar.gz
gitblit-3079105705d76ff0fbfcc755825aa75b0b6ef01a.zip
Set Link: <url>; rel="canonical" http header for SEO (issue-304)
Change-Id: Ie023b5fc58bf988e9064700918a79718aaa6c94b
-rw-r--r--releases.moxie1
-rw-r--r--src/main/java/com/gitblit/wicket/pages/BasePage.java13
-rw-r--r--src/main/java/com/gitblit/wicket/pages/RepositoryPage.java8
3 files changed, 16 insertions, 6 deletions
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: <url>; 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<? extends BasePage> 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));
}
}