Web Page Redirects with .htaccess and Apache
When I’m presenting at a conference, or sharing a project on social media, I like to have a short, easy to remember URL to share.
While it may not be the easiest way, what works for me is creating a URL redirect with whatever short, easy to remember the link I want, to the long-form blog post, landing page, etc.
For example, back when conferences were held in the physical space, I would host the slides for my talk, related resources, sample code, etc as a blog post on my website.
The slug or full URL for the blog post was usually the title of the talk plus the name of the conference or year. While this was a good idea from an SEO perspective, it sometimes made for a long URL that is difficult to fit onto a presentation slide.
Instead, using a web page redirect, I could use shannoncrabill.com/conference-name
in my presentation which would direct viewers to the full blog post or resource.
Here’s how I did it.
Setting up the Redirect
With my current set up, I have access to the root directory where my personal website is hosted. This is where a .htaccess
file will live, which allows the Apache webserver to do the work for us.
The dot (.) at the front of the .htaccess
file may mean you may have to turn on show hidden files in your FTP client. If one does not exist, you can create one. If you already have one, you can add new redirects to the same file.
Creating the redirect has three parts.
- The keyword Redirect and HTTP status code (301 in our case)
- The URL or domain that you want to redirect from
- The URL, domain, subdomain, folder, etc you want to redirect to
Let’s say, I wanted the URL shannoncrabill.com/poodle
to redirect to a blog post with the URL shannoncrabill.com/why-poodles-are-the-best-and-worst-dogs-ever
, I’d add a line to my .htaccess
file that looks like this.
Redirect 301 /poodle https://shannoncrabill.com/blog/why-poodles-are-the-best-and-worst-dogs-ever/
The HTTP status 301 signals to that the requested webpage has been moved permanently. It would be suitable to use if your entire site was moving from one domain to another or in this case if we want to redirect to a specific page.
Once the .htaccess
has the updates you want, push it live to your webserver, and give it a test.
What I like about this approach is that it gives me control over the URL’s that are getting shortened as opposed to using an external service.