Redirect old URLs to new URLs with .htaccess
Description: Redirect /old-page/ to https://example.com/new-page/ with a 301, and redirect all http traffic to https.
Hint:
- Use
Redirect 301for one page, andRewriteRulewith a condition for https.
Solution
Redirect 301 /old-page/ https://example.com/new-page/
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
How it works
- The first line is a permanent redirect for one URL.
- The rewrite rule runs when the request is not https and sends the same path to https.
- After you save the file, check with
curl -Ithat the status is 301 and that the Location line is correct. This file needs the Apache server with mod_rewrite enabled.
Read Redirection.
Learn more about the similar topics:
| Tutorials |
|---|
| SEO – Redirection |
| SEO – Custom 404 Page |
| Exercises & Assignments |
|---|
| Redirect old URLs to new URLs with .htaccess |
| Interview Questions & Answers |
|---|
| What is the difference between a 301 and a 302 redirect? |
| What is a 404 error and a soft 404? |
| How do you do a website migration without losing SEO? |