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 301 for one page, and RewriteRule with 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 -I that 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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.