How to Set Up Permanent 301 Redirect Using .htaccess


set up permanent 301 redirect using .htaccess

A Permanent 301 Redirect allows you to automatically redirect visitors and search engines from old links to new pages. It allows search engines to know that your pages have been moved to new location. You can set up permanent 301 redirect using .htaccess file in Apache server.

Here are some common use cases to set up permanent 301 redirect using .htaccess:

  • Redirect specific URLs to other locations on same domain
  • Redirect all pages from old domain to new domain
  • Force www version (or non-www version) of domain to be used
  • Redirect all files with specific extension to another extension

Let us take a look at each use case one by one

 

How to Set Up Permanent 301 Redirect Using .htaccess

 

Redirect individual URLs and files

To redirect individual URL like example.com/old-url to /new-url on same domain


Redirect 301 /old-url /new-url

 

To redirect individual file like example.com/oldfile.html to /newfile.html on same domain


Redirect 301 /oldfile.html /newfile.html

 

To redirect individual URL like example.com/old-url to newexample.com/new-url on different domain


Redirect 301 example.com/old-url newexample.com/new-url

 

To redirect individual file like example.com/oldfile.html to newexample.com/newfile.html on different domain


Redirect 301 example.com/oldfile.html newexample.com/newfile.html

 

Redirect All Pages from old domain to new domain

To redirect all pages from old domain (example.com) to new domain (newexample.com), you need to add the following lines in the .htaccess of your old domain (example.com)


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

This will set up 301 redirect for your entire old domain

 

Force www version of domain to be used

Search engines like Google see the www and non-www versions of your website as two separate websites altogether. So when people link to non-www version of your site and you want visitors to reach the www version instead, you can force that version of your domain.


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

 

Force non-www version of domain to be used

On the other hand, if you have many links to www version and want people to reach the non-www version, you can force this version of your domain


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

 

Redirect all files with certain extension

If you want to redirect all files of one extension, say, .php to another extension, say, .htm, then you can do it using .htaccess file


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

 

Please restart your Apache server to apply the changes


$ sudo systemctl restart apache2 [Ubuntu/Debian]
$ sudo systemctl restart httpd [RHEL/CentOS]

 

 

 

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!