SEO

Redirect Only Root Directory or Index Page with .htaccess

Previously, we looked at on how to Redirect Everything Except Root Page with .htaccess.

I have another domain where I need to redirect only root directory or index of the domain but not the sub-directory. You may need this is you have cPanel & you need to redirect the main domain without affecting your add-on domain.

To do this, I have to edit the .htaccess file in the root domain & add the following code. Remember to replace “yourolddomain” & “yournewdomain” with your domain name before using it.

RewriteEngine on
RewriteCond %{HTTP_HOST} yourolddomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://yournewdomain.com/ [L,R=301]

Redirect Everything Except Root Page with .htaccess

Recently, I moved my website to a different domain. For good SEO, we should redirect everything, including path to a new domain.

However, due to some reason, I need to maintain the root (index page) page, but I can redirect all other URL from the old to a new domain.

To do this, I have to edit the .htaccess file in the root domain & add the following code. Remember to replace “yourolddomain” & “yournewdomain” with your domain name before using it.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?yourolddomain\.com$ [NC]
RewriteRule ^.+$ http://yournewdomain.com%{REQUEST_URI} [L,R=301]