URL redirect / forwarding

Is it possible to define URL redirects? Like forwarding /cv.pdf to another URL instead of hosting the file in static/?

1 Like

You could do this with mod_rewrite in your /static/.htaccess file.

That’s interesting. Does Hugo implement .htaccess? Or will it only work when the files are eventually hosted by Apache? What about nginx?

Does GitHub Pages run Apache with .htaccess support?

Also, couldn’t Hugo in theory have a feature for URL redirects by generating pages like this

<meta http-equiv="refresh" content="0; url=http://example.com/" />

Could be as simple as having a [redirects] section in the config file.

Using .htaccess is independent of Hugo, but it does depend on the HTTP server you’re using. nginx should have its own equivalent solution, and I’m not sure about GitHub Pages.

As for META redirects, Hugo does have aliases, but I think these work only for content pages, not for other links.

Could there be interest for extending the aliasing functionality to be more general? Like having an aliases.toml file or an [aliases] section that is always checked and alias pages generated for it?

This could be nice for people that want my.domain/github, my.domain/facebook URL’s or the like and manage them from Hugo.

1 Like

No, that does not sound like a good idea. You might (with emphasis on might) be able to do it in the future with the multiple output feature I sketched in another thread. But we are not building a URL shortener or whatever.

Also see Vanity Imports with Hugo · npf.io

@stevenroose try Netlify - great host, great support (no affiliation). Using Netlify you essentially put a file called _redirect in your static folder and format like this:

 # This is a comment. Below, old url is followed by new url and status code
oldpage newpage 301
second third 302

Meta refresh has historically been a bad way to do redirects. Search engines prefer to get http header response 301 for pages that have been permanently moved

3 Likes

Just to note that meta refresh have been and still is a fine way to do redirects, but they have had sub-optimal SEO performance (search engines).

I’m disappointed that there seem to be no plans for supporting non-content redirects—this is a pretty common thing to need when you’re migrating from another site generator or CMS to Hugo.

@raxod502, like @RickySpanish said have a look at Netlify, they have a free plan you can try and most redirects can be done. God I sound like a plugger, but it is a good host for SSGs.

1 Like

Thank you for the suggestion, but I don’t want my site to be tied to a particular hosting provider. Even if I end up choosing Netlify, I really think this is something that makes more sense to be done on the SSG side. (After all, Hugo already does content redirects—non-content redirects would be exactly the same and I can’t really understand why they’re not supported.)

+1 for this -

would be a great feature to add redirect support at a global level (Jekyll allows you to define redirects in the config file).

You can use the Custom Output feature in Hugo to write redirects to one or more .htaccess or whatever file format supported by your web server.

What is the definition of “a redirect” in Jekyll? Similar to Hugo aliases?

Jekyll redirects, from what I can tell, are just meta refreshes:

It’s a plugin and uses redirect_from: as a field in front matter. I don’t see how this is any better than Hugo’s native alias support. It is also semi-specific to GitHub pages, which doesn’t allow for customer-defined redirects…

I’m trying to do just this for the expired pages of an events section. Is there a way to get the list of a section’s expired pages in its list template? They don’t appear to be contained in AllPages. I see there’s a rawAllPages that looks like it might contain them, but it’s not exported…

hugolib/page_collections.go:

// Includes absolute all pages (of all types), including drafts etc.
rawAllPages Pages

Update: I filed Allow templates access to expired and draft pages · Issue #4026 · gohugoio/hugo · GitHub for this. @bep, If I could get some guidance on a preferred approach, I could take a stab at a pull request.

@bep With page bundles released, I was wondering if you might have time to comment on this one now? I’m hoping this will be a low-cost change relative to the reward.

There is nothing new in bundles vs this. We have one more type (headless) that you cannot iterate directly so that maybe motivates some kind of design. But is needs some kind of proposal with an uniformed API. I’m not adding .Site.ExpiredPages etc.

try aliases, you may not need server-side URL forwarding then

# page1.md
---
aliases:
    /page1/
    /post/page1/
---