Practical advice for frontend engineers / web developers
Sometimes it’s useful to be able to redirect the homepage of a site to an already existing page. The main case is when a site is being built a piece at a time, and the first piece is ready. With our limited time on our own projects we sometimes need the motivation of having something publicly available without having to complete everything.
Typically the first feature to be ready is a blog, one that keeps track of where we are in building the website, and a starting point for a conversation and a community.
Eventually the blog will just be one small piece of the final puzzle, so it makes sense to put that blog in it’s proper end destination from the very start, and not just let it be the entire website and then have to remap URLs as the site blossoms.
With .htaccess we can redirect requests to the homepage to where the blog is set up. We need to do this temporarily, because when the rest of the site is completed – or enough of the site to warrant a homepage – we need some way of turning off the redirect.
Adding in the following configuration into the .htaccess file of the root of the webserver (or more ideally in the httpd.conf):
# Redirect homepage page to /blog/ (temporarily) RedirectMatch temp ^/$ http://example.com/blog/
The RedirectMatch uses regular expressions, so we can match just a single / (regex
). The Redirect directive treats the start URL as a prefix, not as a full URL, which means redirecting just / is not straightforward.
The destination is a fully qualified URL, which means the domain name needs to be included. This is normal, because a proper HTTP redirect response requires a fully qualified HTTP URL on the Location HTTP Header.
Although the Apache redirect default is a Temporary redirect, I’ve explicitly specified a Temporary redirect with the
status keyword. This is just to make it absolutely clear to human beings that I really want a temporary redirect.
The advantage of the temporary redirect is that we have the control to turn it off in the future. So when we have a working homepage ready we can just remove the redirect and people visiting our site will then see the newly built homepage.
Although we can remove a permanent redirect setting from the .htaccess just as easily, this won’t be as clean. Permanent redirects imply that the start URL will never be anything other than a redirect. So intermediary cache servers and browsers remember these, so each time a browser user goes to the original URL the browser or the intermediary cache notices a Permanent redirect was specified for this URL the last time and it redirects the visitor to the destination URL. In these cases our web server has no say in the matter. The network believes us when we say a redirect is permanent.