htaccess redirect Domain & sub Domain
0 [like_dislike_count_code_webnius]

htaccess redirect Domain & sub Domain

Migrating a website hosting never effect off-page SEO but migrating a domain name to new one always effect SEO and ranking factors. Changing URL of website to new one always lead to Ranking destruction . In that case what best can be done to bring the user back to new URL is 301 permanent redirect. So redirection may give traffic to new URL.I suggest changing of URL should never be done until and unless it’s absolutely necessary. Changing of URL destruct ranking completely because old ranked URL returns 404 NOT FOUND page to google. Although ranking is reversed back after certain time but I personally suggest we should never follow URL change procedure without any urgent requirement.

Forwarding a domain or sub domain is almost equivalent to redirecting a domain or sub domain. we can use these words interchangeably. In this article , assuming that readers already understand well about domain name , sub domains and URL, we are going to cover complete guidance on redirecting/forwarding a URL.

well , Many developers introduce domain forwarding as server side request. The server forwards the request of user to another page. For example If you forward example.org to example.com then the server will handle this request on its end. And they introduce domain/sub-domain redirect as client side operation. It means client telling website visitors to go elsewhere. In this article , we will provide maximum possible codes of redirecting / forwarding the URL using server sided file called HTACCESS.

Actually Using Redirect/forwarding in an .htaccess file enables you to redirect users from an old url to a new url instantly. This redirect will be very sudden and fast that we won’t even notice the old path page structure or DOM tree , because its happening on server side. For example, if you use /path/index.html as your old path and then later set new path as /path/home.html, so in that case we can enable redirect to send visitors from old path to new path.

Let’s move ahead with HTACCESS codes now. Below is the maximum possible codes of forwarding / redirecting a domain or sub domain.

Using htaccess (htaccess codes to be used in .htaccess file located on server)

1) Redirect a Domain name to another Domain name (we can say redirect a Domain name URL to another Domain URL)
Below code will redirect all pages to new domain. For example

www.example.net/somepage.html redirects to www.newdomain.com
www.example.net redirects to www.newdomain.com
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^.*cerinsch\.com$ [NC]
RewriteRule ^(.*)$ http://webnius.com/ [R=301,L]


2) Redirect a domain name to another domain name but changes only the domain name path , rest URL remains the same. For example

www.example.net/somepage.html?var=foo redirects to www.newdomain.com/somepage.html?var=foo
RewriteEngine on 
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]


3) Redirect a domain name to sub-domain name but changes only the domain name to sub-domain name , rest URL remains the same. For example

www.example.net/somepage.html?var=foo redirects to new.newdomain.com/somepage.html?var=foo
RewriteEngine On
RewriteRule (.*) http://new.domain.com/$1 [R=301,L]


4) Redirect a sub-domain name to a folder on the root domain that uses the same name. For example

http://abc.domain-name.com redirects to http://www.domain-name.com/abc
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.yourdomain\.com
RewriteRule ^(.*)$ http://www\.yourdomain\.com/subdomain/$1 [L]


5) Redirect a sub-domain name to a main domain name but here also path of URL will remain same. For example

http://blog.webnius.com/demo.html/ redirects to http://www.infoconic.com/demo.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.example\.com  [NC]
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]


6) Redirect any non www to www AND domain name to folder in root (dual work)
For example

http://webnius.com redirects to http://www.infoconic.com/any_folder
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/webnius/
RewriteRule ^(.*)$ /webnius/$1 [L]


7) Redirect any domain name to folder in root.(without non www to www) For example

http://www.infoconic.com redirects to http://www.infoconic.com/any_folder
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomain.com$
RewriteRule ^(/)?$ any_folder [L]


8) Redirect any sub-domain to folder in root.(when sub-domain already pointing to root).
For example

http://demo.webnius.com redirects to http://demo.webnius.com/any_folder
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.com
RewriteCond $1 !^folder-name
RewriteRule (.*) /folder-name/$1 [L]


9) Redirect specific URL to new URL(on same server)
For example

/path/index.html redirects to /path/home.html
Redirect /path/to/old/file/old.html /path/to/new/file/new.html


10) Redirect specific URL to new URL(on external server)
For example

/path/index.html redirects to http://www.example.com/new/file/new.html
Redirect /path/to/old/file/old.html http://www.example.com/new/file/new.html


11) Redirect from an index.html file to a different directory
For example

/index.html redirects to /new/
Redirect /index.html /new/

Something To Say ?

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

*