htaccess – Common Redirects
Anytime you migrate a website, or even just move a single page to a new URL, redirects aid search engines in properly indexing your site’s content. They come in two main flavors, 301 (Permanent) or 302 (Temporary). For more info on the types of redirects click here. The following is the basic syntax for redirects in Apache:
Redirect [status] "old-URL-path" "new-URL"
- Make sure you capitalize the
RinRedirector it won’t work. Everything is case sensitive. - The status is optional and is usually a number indicating the HTTP status code you want to deliver to the browser. You can use the word
permanentin the place of301, ortempin the place of302. If not provided, then302will be used as the default. - You can include the quote marks but they are not needed for any redirect to work.
- The URL-path is required and is always a path relative to the site root, not the location of the
.htaccessfile. - The URL is required and is either a path relative to the site root, assuming the redirect is within the same site, or an absolute URL if the redirect points to another site.
To add a standard redirect to your site copy one of the following redirects into your htaccess file and adjust the file name / directory name / domain name as needed. To add more than one redirect simply copy the redirect line and paste it below the first redirect and edit as needed.
301 Page Redirect
This is a standard 301 (permanent) single page redirect.
# BEGIN 301 Page Redirects
Redirect 301 "/old-page.php" "https://www.mydomain.com/new-page.php"
301 Directory Redirect
This is a standard 301 (permanent) redirect for an entire directory… yep we can do that too!
# BEGIN 301 Directory Redirects
Redirect 301 "/old-directory" "https://www.mydomain.com/new-directory"
Sources:
https://httpd.apache.org/docs/current/mod/mod_alias.html#Redirect

Leave a Reply
Want to join the discussion?Feel free to contribute!