Frankfurt am Main, 1 October 2021The management team of the central registry and technical operator of the German country code Top Level Domain (...)
Beginner’s Guide to htaccess Redirect
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 mappingURL 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 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### Redirect Directive Syntax # Redirect [status] [URL_path] URL ### Example Redirect 301 "/old_url.html" "/new_url.html" Here’s the redirect directive syntax:
- 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 argumentThis 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”