hugo-redirect
hugo-redirect copied to clipboard
A theme components to enable easy redirection in Hugo sites
Hugo Redirect
A theme component to enable easy redirection in Hugo sites.
About
Hugo Redirect enables easy redirection: one of the major pieces missing from Hugo's impressive feature set.
Let's say you
- Have an Ugly URLs like
yoursite.com/cv.pdfand you'd much rather point people toyoursite.com/cv - Wrote an awesome post on
yoursite.com/blog/2019/09/08/how_to_add_clisand you'd like to share it to people quickly atyoursite.com/cli
Redirection comes in very handy in these cases.
Hugo Redirect currently supports static meta refresh based redirects, _redirect generation for Netlify and and .htaccess (for Apache / Nginx servers) generation.
This is not a standalone theme. It is a Hugo theme component (sort of like a plugin) providing easy URL redirect capabilities to Hugo sites. A working demo of this redirection is available on my site at https://prag.io/cv.
Contributions welcome! Send your pull request.
Usage
In the root of your site repository:
- Add
hugo-redirectas a submodule to be able to get upstream changes later$ git submodule add https://github.com/gcc42/hugo-redirect.git themes/hugo-redirect - Add
hugo-redirectas the left-most element of thethemelist variable in your site's or theme's configuration fileconfig.yamlorconfig.toml. Example, withconfig.yaml:
or, withtheme: ["hugo-redirect", "other-components", "my-theme"]config.toml,theme = ["hugo-redirect", "other-components", "my-theme"] - To add a new redirect rule, simply run (It's not recommended to create
them manually):
Open the newly created file$ hugo new redirect/cv.md # Replace cv with a (arbitrary) redirect nameredirect/cv.mdin your editor and update theurlandredirect_tofields in the front matter, like so:type = "redirect" url = "/cv" redirect_to = "/cv.pdf" redirect_enabled = true - If you're hosting on Netlify or Apache/Nginx/<Service that supports
.htaccess>, follow the steps below to enable_redirectsfile generation (recommended)
Hosting on Netlify
If you're using Netlify, you'll want to generate the native _redirects file:
- Copy
_redirects.mdto your content folder:$ cp themes/hugo-redirect/content/_redirects.md content/ - Edit the file in your text editor and update the value of
draftfromtruetofalse
Hosting on Apache / Nginx
Check if your hosting supports the .htaccess file configuration. If they do, enable it as:
- Copy
_htaccess.mdto your content folder:$ cp themes/hugo-redirect/content/_htaccess.md content/ - Edit the file in your text editor and update the value of
draftfromtruetofalse
That's it. You're done. Now simply build and deploy your site and any requests to yoursite.com/cv should be redirected to yoursite/cv.pdf.
Things to note
- I'd recommend enabling the appropriate
_redirects/.htaccessbased on where you're hosting. (Even thoughmetaredirects work fine, this will potentially improve the speed and help with SEO) - Avoid mixing
hugo-redirectwith manual redirection, or you could end up creating a nasty redirect loop - Make sure you enter the
urlandredirect_toparameters exactly as you want them. This means that if you want/cv --> /cv.pdf, make sure you seturl = /cvand NOTurl = /cv/(however because of the way Hugo works currently, both/cv --> /cv.pdfand/cv/ --> /cv.pdfwill be set up)
Questions?
Create an issue or email me at pranjal at prag.io.
Copyright © 2012 onwards, Pranjal Agrawal [email protected].