How to redirect a website with Htaccess?

Redirect a Website

Shifting Entire Site to New Domain

Redirect 301 / http://www.example.com/

Redirect Single File

Redirect 301 /old-path.php /new-path.php

Redirect Entire Site to One Page

RedirectMatch 301 ^/ /offline.php

Changing File Extension

RedirectMatch 301 (.*).html$ http://www.example.com$1.php

Redirect WWW to Non-WWW

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

Redirect Non-WWW to WWW

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

Redirect Codes:

  • 301: Permanently Moved
  • 302: Temporarily Moved
  • 303: Replaced and Refer New
  • 305: Use Proxy
  • 410: Permanently Removed

Leave a comment

Your email address will not be published. Required fields are marked *