]> source.dussan.org Git - gitblit.git/commitdiff
Set Link: <url>; rel="canonical" http header for SEO (issue-304)
authorJames Moger <james.moger@gitblit.com>
Fri, 25 Oct 2013 16:18:23 +0000 (12:18 -0400)
committerJames Moger <james.moger@gitblit.com>
Fri, 25 Oct 2013 16:18:23 +0000 (12:18 -0400)
Change-Id: Ie023b5fc58bf988e9064700918a79718aaa6c94b

releases.moxie
src/main/java/com/gitblit/wicket/pages/BasePage.java
src/main/java/com/gitblit/wicket/pages/RepositoryPage.java

index e9ec642dff10306a56b9cf7e1cc23461f7589c3c..403d34fe1ad544893d612257e333ead527714ac0 100644 (file)
@@ -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
index cb9de9fa02e0c5e6985f6da4573b96e013ce9ecf..f8816831917683629bdec033455ed61c3d22d4d2 100644 (file)
@@ -88,6 +88,16 @@ public abstract class BasePage extends SessionPage {
                }\r
        }\r
 \r
+       protected String getCanonicalUrl() {\r
+               return getCanonicalUrl(getClass(), getPageParameters());\r
+       }\r
+\r
+       protected String getCanonicalUrl(Class<? extends BasePage> clazz, PageParameters params) {\r
+               String relativeUrl = urlFor(clazz, params).toString();\r
+               String canonicalUrl = RequestUtils.toAbsolutePath(relativeUrl);\r
+               return canonicalUrl;\r
+       }\r
+\r
        protected String getLanguageCode() {\r
                return GitBlitWebSession.get().getLocale().getLanguage();\r
        }\r
@@ -129,6 +139,9 @@ public abstract class BasePage extends SessionPage {
 \r
        @Override\r
        protected void setHeaders(WebResponse response) {\r
+               // set canonical link as http header for SEO (issue-304)\r
+               // https://support.google.com/webmasters/answer/139394?hl=en\r
+               response.setHeader("Link" ,MessageFormat.format("<{0}>; rel=\"canonical\"", getCanonicalUrl()));\r
                int expires = GitBlit.getInteger(Keys.web.pageCacheExpires, 0);\r
                if (expires > 0) {\r
                        // pages are personalized for the authenticated user so they must be\r
index 508b8654821e420f2838608ae09c8be5199a0483..68aa9a7ab1a503f45e27d5a95556770df58108a5 100644 (file)
@@ -39,7 +39,6 @@ import org.apache.wicket.markup.html.link.ExternalLink;
 import org.apache.wicket.markup.html.panel.Fragment;\r
 import org.apache.wicket.model.IModel;\r
 import org.apache.wicket.model.Model;\r
-import org.apache.wicket.protocol.http.RequestUtils;\r
 import org.apache.wicket.request.target.basic.RedirectRequestTarget;\r
 import org.eclipse.jgit.diff.DiffEntry.ChangeType;\r
 import org.eclipse.jgit.lib.PersonIdent;\r
@@ -721,9 +720,7 @@ public abstract class RepositoryPage extends RootPage {
                        String searchString = searchBoxModel.getObject();\r
                        if (StringUtils.isEmpty(searchString)) {\r
                                // redirect to self to avoid wicket page update bug\r
-                               PageParameters params = RepositoryPage.this.getPageParameters();\r
-                               String relativeUrl = urlFor(RepositoryPage.this.getClass(), params).toString();\r
-                               String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl);\r
+                               String absoluteUrl = getCanonicalUrl();\r
                                getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl));\r
                                return;\r
                        }\r
@@ -745,8 +742,7 @@ public abstract class RepositoryPage extends RootPage {
                        // use an absolute url to workaround Wicket-Tomcat problems with\r
                        // mounted url parameters (issue-111)\r
                        PageParameters params = WicketUtils.newSearchParameter(repositoryName, null, searchString, searchType);\r
-                       String relativeUrl = urlFor(searchPageClass, params).toString();\r
-                       String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl);\r
+                       String absoluteUrl = getCanonicalUrl(searchPageClass, params);\r
                        getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl));\r
                }\r
        }\r