Posted by Fabrizio on October 15, 2020
To say the .htaccess redirect function is “useful” is an understatement. This redirect functionality from Apache web servers will make your job as webmaster astonishingly easy. That’s why as soon as you’ve selected a domain and built your website using a website builder, the next thing on your to-do list should be mastering the .htaccess redirect.
This is a guide for beginners, so we’ll walk you through everything gently.
Before we get into all the things you can do with .htaccess redirect, let’s tackle the most important question:
What is .htaccess redirect?
.htaccess redirect is a function that allows you to redirect visitors from an old page to a new one without having to keep the old one. So, for example, if you have a webpage file called toptenfrogs.html and you later rename it to toptensqirrels.html, you can set up a redirect to send users from toptenfrogs.html to toptensquirrels.html.
If you have a website, it’s crucial that you understand how to use .htaccess redirec because it has a direct impact on server load, site speed and user experience.
The first thing we need to know about in order to understand .htaccess redirect is URL mapping.
First, URL mapping
URL mapping is the process we use to match a URL to a resource file in the server filesystem. But thanks to CMS like WordPress, webpages aren’t static html files anymore, so the process of URL mapping has gotten a lot more complicated.
But here are the basics:
When your browser requests a URL, your query is passed to DNS servers, which perform a lookup and send a request to the web server. The web server then figures out what to do, whether that’s loading the webpage or loading a page that displays an error message.
The most basic way for the Apache server to do URL mapping is to match the path and file to a directory and file in the DocumentRoot.
So, for example, let’s look at what a URL mapping would look like for a DocumentRoot on catswithmessyfur.com:
The DocumentRoot for catswithmessyfur.com on your server is set to the path pat/www/html.
A request for http://catswithmessyfur.com/insertpath/file.html would be matched to pat/www/html/insertpath/file.html in the server file system.
Easy as pie.
But what happens when the files are stored outside the DocumentRoot or you don’t want a direct URL to resource match? This could happen if, for example, the file doesn’t have a “.html” at the end. This is what happens if you’re using a CMS like WordPress.
In this case, we need to use the two modules that Apache uses to map URLSs to resources that don’t match the filesystem of the requested URL. These modules are mod_alias and mod_rewrite.
mod_alias is great for simple redirects, and it’s probably the version of .htaccess redirect you’ll start out using. Here are some directives that are specific to mod_alias:
### Redirect Directive Syntax
# Redirect [status] [URL_path] URL
Redirect 301 "/old_url.html" "/new_url.html"
Here’s the redirect directive syntax:
Each of these items after the Redirect directive is called an argument. Let's take a closer look at each:
- Redirect is the directive.
- [status] identifies the HTTP status code to be served.
- [URL_path] is the path of the URL to be redirected.
- URL is the new URL to be served.
1. The status argument (optional)
You can choose not to include the status argument. Just know that the default status for the Redirect directive is 302, which means if you don’t specify an HTTP status, Apache will serve a status of 302.
For example, these two redirect statements will have the same effect:
### Same redirect in different words
Redirect "/old_URL_path" "/new_URL_path"
Redirect 302 "/old_URL_path" "/new_URL_path"
2. The URL-path argument
This cannot be a relative URL. This means that you must include the slash at the beginning of the URL.
This is correct:
Redirect “/old_URL.html” “/new_URL.html”
This is incorrect:
Redirect “old_URL.html” “/new_URL.html”
3. The URL argument
Like the URL_path argument, this must begin with a slash.
Redirect "/old_url.html" "http://www.catswithmessyfur/new_url.html"
If the status argument is between 300 and 399, the URL argument must also be present.
If the status argument is NOT between 300 and 399, the URL argument must also NOT be present.
Now, let’s look at mod_rewrite.
mod_rewrite is a bit more flexible than mod_alias. (Once you start using them both, you’ll see how big an understatement this is.)
The syntax for mod_rewrite is a smidge more complicated, but once you figure out how to use it, you’ll be able to manipulate your website on a whole new level.
Great uses for mod_rewrite
mod_rewrite “rewrites” URLs. Here are three things you can do with it:
1. Hiding your hideous URLs
Sometimes, a website’s URL-naming convention is great for the webmaster but horrible for visitors. Let’s say, for example, that I have a URL called http://www.catswithmessyfur.com/ display_images.php?imageId=blueberry_jam.
This is great for my own internal records, but a user won’t be able to look at that URL and know at a glance what webpage they’re looking at.
So, instead of showing my visitors http://www.catswithmessyfur.com/ display_images.php?imageId=blueberry_jam, I might want them to see http://catswithmessyfur.com/blueberryjam.html.
2. Avoiding 404 errors during site reorganisation
When you move webpages around, you can use mod_rewrite to redirect visitors to the new location, so they don’t get a “404 Page not found” error when they type in the old URL.
3. Redirecting to your main URL
Sometimes your site has multiple URLs. For example, you may have bought multiple misspelled versions of your domain names to redirect your more phonetically challenged visitors to your actual website. If your website has multiple URLs, you can use mod_rewrite to redirect all your visitors to your main URL.
As you learn more about .htaccess redirects, you’ll learn the specific syntax for performing all these functions and more. You’ll love being able to reorganise your website without losing traffic, redirect spelling-challenged visitors to your main website, and make your URLs as ugly as you please, then redirect your visitors to a prettier one. Once you have a good handle on the syntax, the possibilities are endless.