How to add 301 redirects with htaccess on Apache
In most cases, a web server running the Apache Web Server software’s behaviour is configured using a .htaccess file. A .htaccess file is a simple text file that sits in the root directory of a website site and it is used to enable and/or disable Apache’s features and functions, including redirect functionality on a site by site level. That allows for multiple sites on the same server to have different configurations.
In order to implement a htaccess 301 redirect the following code will need to be added directly to the site's .htaccess file...
“redirect 301 /old.html https://www.redirectlyapp.com/new.html”
- The "redirect 301" tells Apache that the following information is a redirect for htaccess and will forward one URL to the other. Web crawlers and web browsers will also receive a "301 response" when trying to access the old destination
- The “/old.html” provides the original location of the page
- The “http://www.domain.com/new.html” is the new URL of the page and is where the server should redirect visitors to. Please note: this part of the code requires the full URL to be entered in its entirety including “http://” or “https://”, depending on whether you have a secure website (which you definitely should!) for a htaccess https redirect.
It is important to note that if you are moving multiple pages, you will need to include a redirect statement for every URL you’re moving. Rather than doing this line by line, you can use the Apache module mod_rewrite (which is installed by default on most Apache installations) to match specific patterns for entire folders and redirect them to their new URLs without having to go through every PHP script.
The mod_rewrite module operates in per-server context or in per-directory context. The per-server context requires that a developer must edit the apache configuration file, httpd.conf at the very top level. The per-directory context uses .htaccess files that exist in each folder a user wants to configure so different sites on the same server can have different configurations.
Redirectly makes it easier when it comes to htaccess redirects with its auto-match functionality that automatically matches the same URLs, and leaves those that don’t have a matching path for the user to drag and drop to the new URL. It’s possible to dictate whether the URL is a 302 (temporary) or 301 (permanent) redirect, too.
When it comes to exporting the redirect list, it can be exported specifically for Apache making it easier to add the new code to the websites .htaccess file, without having to do it manually or using tools such as Rewrite Rule Generator for batch rewrites of URL code. You’ll be able to copy all of the redirects from Redirectly and paste them straight into your htaccess seamlessly for a htaccess redirect for 301s.