14+ years building on WordPress / Replies in under 5 hours
WordPress 1 min read · Updated July 2026

Permanent 301 Redirect using .htaccess

AK
Ajay Khandal
WordPress Developer
TL;DR

A 301 redirect tells search engines and visitors that an old URL has permanently moved to a new one, preserving your SEO value instead of losing it to a broken link. This post gives copy-paste .htaccess code snippets for redirecting individual files, redirecting an entire old domain to a new one, and forcing a consistent www or non-www version of your site. These are quick fixes you can apply directly, though a developer should verify them on your specific server setup to avoid redirect loops.

Permanent 301 Redirect using .htaccess

301 redirect tells search engine and other users that old link is replaced by new link. There are many different kind of 301 redirection.

Redirect individual files on the same domain

To make this kind redirection you need to open .htaccess file and put this code.

Redirect 301 /oldfile.htm /newfile.htm

Redirect individual files on the other domain

Redirect 301 /oldfile.htm http://demo.net/newfile.htm

Redirect an old domain to a new domain

RewriteEngine on
RewriteCond %{HTTP_HOST} ^demo.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.demo.com [NC]
RewriteRule ^(.*)$ http://demo.net/$1 [L,R=301,NC]

301 Redirect

Force www. version of domain to be used

RewriteEngine on
RewriteCond %{HTTP_HOST} ^demo.com [NC]
RewriteRule ^(.*)$ http://www.demo.com/$1 [L,R=301,NC]

Force non www. version of domain to be used

RewriteEngine on
RewriteCond %{HTTP_HOST} ^demo.com [NC]
RewriteRule ^(.*)$ http://www.demo.com/$1 [L,R=301,NC]

Redirect all files with certain extension

RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.htm [R=301,L]

Looking to round out your WordPress knowledge? Learn more about how to resolve the too many redirects error in WordPress. Learn more about how to fix a 500 Internal Server Error in WordPress.

AK

Written by Ajay Khandal

WordPress Developer — building, fixing and speeding up WordPress sites.

Work with me →