

Rewrite: A Simple and Powerful URL Rewriter for Go
source link: https://dev.to/kataras/rewrite-a-simple-and-powerful-url-rewriter-for-go-224h
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rewrite is a Go package that lets you rewrite URL paths, subdomains or hosts based on regular expressions. It is inspired by Apache's mod_rewrite module and can be used as a middleware for net/http.
Rewrite can help you achieve better SEO results by redirecting old or unwanted URLs to new ones, enforcing a primary subdomain, or simplifying complex URL structures.
How to Install Rewrite
To install Rewrite, you need to have Go installed on your system. Then, you can use the go get command to download and install the package:
$ go get github.com/kataras/rewrite
How to Use Rewrite
You can load rewrite options in two ways:
- Through code using the
New
function andHandler
method. This way, you can handle parse errors and create rules programmatically. - Or through a YAML file using the
Load
function which returns afunc(http.Handler) http.Handler
. This is the most common and simplest way. It panics on parse errors.
The syntax of the rewrite rules is:
REDIRECT_CODE_DIGITS PATTERN_REGEX TARGET_REPL
Where:
-
REDIRECT_CODE_DIGITS
is the HTTP status code to use for the redirection, such as 301 (Moved Permanently) or 302 (Found). -
PATTERN_REGEX
is the regular expression that matches the request URL path, subdomain or host. -
TARGET_REPL
is the replacement string that contains the new URL path, subdomain or host.
For example:
301 /seo/(.*) /$1
This rule redirects all requests from relative path /seo/*
to /*
using the 301 HTTP status code.
You can also use a special option called PrimarySubdomain
to redirect all requests from the root domain to a specific subdomain, such as www
.
For example:
PrimarySubdomain: www
This option redirects all requests from example.com
to www.example.com
.
Examples
Let's write a simple application that follows these redirect rules:
- Redirects
/seo/*
to/*
- Redirects
/docs/v12*
to/docs
- Redirects
/old (.*)
to/
- Redirects root domain requests to
www.
The code would look like this:
package main
import (
"net/http"
"github.com/kataras/rewrite"
)
func main() {
mux := http.NewServeMux()
// [...routes]
// Load the redirect rules from a YAML file.
redirects := rewrite.Load("redirects.yml")
// Wrap the router with the rewrite middleware.
http.ListenAndServe(":8080", redirects(mux))
}
The YAML file would look like this:
RedirectMatch:
# Redirects /seo/* to /*
- 301 /seo/(.*) /$1
# Redirects /docs/v12* to /docs
- 301 /docs/v12(.*) /docs
# Redirects /old (.*) to /
- 301 /old(.*) /
# Redirects root domain requests to www.
PrimarySubdomain: www
That's it! Now you can test your application and see how it redirects the requests according to your rules.
You can find more examples in the _examples
folder of the GitHub repository: https://github.com/kataras/rewrite/tree/main/_examples
Conclusion
Rewrite is a handy package that lets you rewrite URLs in Go with ease. It is compatible with any net/http router or framework and supports regular expressions for flexible matching and replacing. It can help you improve your SEO performance by redirecting old or unwanted URLs to new ones, enforcing a primary subdomain, or simplifying complex URL structures.
If you want to learn more about Rewrite, you can check out its GitHub repository: https://github.com/kataras/rewrite
I hope you enjoyed this article and found it useful. If you have any feedback or questions, feel free to leave a comment below. Thank you for reading! 😊
Recommend
-
154
README.md
-
36
Paraphrasing Tool - Paraphrasing Tool is a free online Article Rewriter tool - NEXT
-
25
GraphQL Query Rewriter Seamlessly turn breaking GraphQL schema changes into non-breaking changes by rewriting queries in middleware. Full API docs are available at
-
32
README.md GraphQL Query Rewriter
-
8
Mod rewrite to delete the file name of the url advertisements If I have a file, let's say rec-view.php, then I can call it by this URL...
-
4
Crazy Magento URL rewrite generation A couple of weeks ago I experienced a Magento URL rewrite behaviour which I wasn’t aware of. Considering that I work with Magento since 2010,...
-
5
例2 替换url中一部分的内容利用正则中的”()和\1 “,替换url中一部分的内容。将http://192.168.19.100/2016/a/b/c/1.html换http://192.168.19.100/2017/a/b/c/1.html
-
8
This is the latest launch from AISEOSee AISEO’s 3 previous launches →
-
7
Kagi 的 url rewrite Kagi 在上個禮拜推出了 url rewrite 的功能,可以把搜尋結果裡面的網址換掉:(在「
-
6
URL rewrite on ExpressJS 4.0 · GitHub Instantly share code, notes, and snippets. URL rewrite on ExpressJS 4.0 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK