{"id":13600258,"url":"https://github.com/kataras/rewrite","last_synced_at":"2025-03-22T04:30:26.206Z","repository":{"id":73636827,"uuid":"314779570","full_name":"kataras/rewrite","owner":"kataras","description":"The rewrite middleware for Go. Perfect for SEO","archived":false,"fork":false,"pushed_at":"2023-11-28T03:30:38.000Z","size":32,"stargazers_count":25,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-01T12:45:10.811Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kataras.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"kataras"}},"created_at":"2020-11-21T09:49:17.000Z","updated_at":"2025-02-18T03:23:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"b6679fcb-bd06-402f-b0ea-8119c536dac5","html_url":"https://github.com/kataras/rewrite","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Frewrite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Frewrite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Frewrite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Frewrite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kataras","download_url":"https://codeload.github.com/kataras/rewrite/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244181377,"owners_count":20411601,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T18:00:33.598Z","updated_at":"2025-03-22T04:30:25.726Z","avatar_url":"https://github.com/kataras.png","language":"Go","funding_links":["https://github.com/sponsors/kataras"],"categories":["开源类库","Go","Open source library"],"sub_categories":["路由","Redirect"],"readme":"# Rewrite\n\n[![build status](https://img.shields.io/github/actions/workflow/status/kataras/rewrite/ci.yml?style=for-the-badge)](https://github.com/kataras/rewrite/actions) [![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/rewrite) [![godocs](https://img.shields.io/badge/go-%20docs-488AC7.svg?style=for-the-badge)](https://pkg.go.dev/github.com/kataras/rewrite)\n\nLike [Apache mod_rewrite](https://httpd.apache.org/docs/2.4/rewrite/) but for Golang's [net/http](https://golang.org/pkg/net/http/). Initially created for the [Iris Web Framework](https://github.com/kataras/iris) a long time ago. The success of its usefulness is well known as many others have copied and moved the [original source code](https://github.com/kataras/iris/tree/master/middleware/rewrite) into various frameworks since then, if you deem it necessary, you are free to do the same.\n\n## Installation\n\nThe only requirement is the [Go Programming Language](https://golang.org/dl).\n\n```sh\n$ go get github.com/kataras/rewrite\n```\n\n### Examples\n\n- [Basic](_examples/basic)\n- [Subdomains](_examples/subdomains)\n\n## Getting Started\n\nThe Rewrite Middleware supports rewrite URL path, subdomain or host based on a regular expression search and replace.\n\nThe syntax is familiar to the majority of the backend developers out there and it looks like that:\n\n| REDIRECT_CODE_DIGITS | PATTERN_REGEX | TARGET_REPL |\n|----------------------|---------------|-------------|\n| 301                  | /seo/(.*)     | /$1         |\n\nWould redirect all requests from relative path `/seo/*` to `/*` using the `301 (Moved Permanently)` HTTP Status Code. Learn more about [regex](https://golang.org/pkg/regexp/#example_Regexp_ReplaceAllString).\n\n**Usage**\n\nFirst of all, you should import the builtin middleware as follows:\n\n```go\nimport \"github.com/kataras/rewrite\"\n```\n\nThere are two ways to load rewrite options in order to parse and register the redirect rules:\n\n**1.** Through code using the `New` function and `Handler` method. Parse errors can be handled and rules can be programmatically created.\n\n```go\n// 1. Code the redirect rules.\nopts := rewrite.Options{\n\tRedirectMatch: []string{\n\t\t\"301 /seo/(.*) /$1\",\n\t\t\"301 /docs/v12(.*) /docs\",\n\t\t\"301 /old(.*) /\",\n\t},\n\tPrimarySubdomain: \"www\",\n}\n// 2. Initialize the Rewrite Engine.\nrw, err := rewrite.New(opts)\nif err != nil { \n\tpanic(err)\n}\n\n// 3. Wrap the router using Engine's Handler method.\nhttp.ListenAndServe(\":8080\", rw.Handler(mux))\n```\n\n**2.** Or through a `yaml` file using the `Load` function which returns a `func(http.Handler) http.Handler`. It is the most common scenario and the simplest one. It panics on parse errors.\n\nThe `\"redirects.yml\"` file looks like that:\n\n```yaml\nRedirectMatch:\n  # Redirects /seo/* to /*\n  - 301 /seo/(.*) /$1\n\n  # Redirects /docs/v12* to /docs\n  - 301 /docs/v12(.*) /docs\n\n  # Redirects /old(.*) to /\n  - 301 /old(.*) /\n\n# Redirects root domain requests to www.\nPrimarySubdomain: www\n```\n\n```go\nfunc main() {\n    mux := http.NewServeMux()\n    // [...routes]\n\tredirects := rewrite.Load(\"redirects.yml\")\n\t// Wrap the router.\n    http.ListenAndServe(\":8080\", redirects(mux))\n}\n```\n\n## Example\n\nLet's write a simple application which follows the redirect rules of:\n\n| SOURCE                                   | TARGET                             |\n|------------------------------------------|------------------------------------|\n| http://localhost:8080/seo/about          | http://localhost:8080/about        |\n| http://mydomain.com:8080/docs/v12/hello  | http://www.mydomain.com:8080/docs  |\n| http://mydomain.com:8080/docs/v12some    | http://www.mydomain.com:8080/docs  |\n| http://mydomain.com:8080/oldsome         | http://www.mydomain.com:8080       |\n| http://mydomain.com:8080/oldindex/random | http://www.mydomain.com:8080       |\n\n### Server\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/kataras/rewrite\"\n)\n\nfunc main() {\n\t// Code the redirect rules.\n\topts := rewrite.Options{\n\t\tRedirectMatch: []string{\n\t\t\t\"301 /seo/(.*) /$1\",       // redirect /seo/* to /*\n\t\t\t\"301 /docs/v12(.*) /docs\", // redirect docs/v12/* to /docs\n\t\t\t\"301 /old(.*) /\",          // redirect /old** to /\n\t\t},\n\t\tPrimarySubdomain: \"www\", // redirect root to www. subdomain.\n\t}\n\t// Initialize the Rewrite Engine.\n\trw, err := rewrite.New(opts)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\trouter := http.NewServeMux()\n\trouter.HandleFunc(\"/\", index)\n\trouter.HandleFunc(\"/about\", about)\n\trouter.HandleFunc(\"/docs\", docs)\n\n\tlog.Println(\"Listening on :8080\")\n\t// Wrap the router using the Handler method.\n\thttp.ListenAndServe(\":8080\", rw.Handler(router))\n}\n\nfunc index(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprintln(w, \"Index\")\n}\n\nfunc about(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprintln(w, \"About\")\n}\n\nfunc docs(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprintln(w, \"Docs\")\n}\n```\n\n### Hosts File\n\n```text\n127.0.0.1\tmydomain.com\n127.0.0.1\twww.mydomain.com\n```\n\nNavigate [here](https://support.rackspace.com/how-to/modify-your-hosts-file/) if you don't know how to modify the system's hosts file.\n\n## License\n\nThis software is licensed under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkataras%2Frewrite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkataras%2Frewrite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkataras%2Frewrite/lists"}