feat(seo): add canonical URL filter to remove query parameters
Addresses community feedback about canonical URLs being misleading when they include UTM parameters. The new Pongo2 filter creates clean canonical URLs while preserving tracking parameters for social sharing.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/flosch/pongo2/v6"
|
||||
)
|
||||
|
||||
func init() {
|
||||
pongo2.RegisterFilter("canonical", filterCanonical)
|
||||
}
|
||||
|
||||
func filterCanonical(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
||||
urlStr := in.String()
|
||||
|
||||
parsedURL, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
// If parsing fails, return the original URL
|
||||
return in, nil
|
||||
}
|
||||
|
||||
// Remove query parameters and fragment
|
||||
parsedURL.RawQuery = ""
|
||||
parsedURL.Fragment = ""
|
||||
|
||||
// Return the cleaned URL
|
||||
return pongo2.AsValue(parsedURL.String()), nil
|
||||
}
|
||||
Reference in New Issue
Block a user