web analytics

Blog

Some common .htaccess domain redirect rules

Tags: .htaccesdomain redirectmod_rewrite

Published on: August 22, 2014 by George K.

Some common .htaccess domain redirect rules

Scenario:

Redirecting a domain or URL is essential to keep the SEO rankings. Below are some of the common htaccess redirect rules which can be helpful to accomplish this.

Old site to New ste 

In the root of your old Web server, edit or create a new .htaccess file using a text editor.

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.old-domain.com$ [NC]

RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=301,L]

A more common scenario is that your new domain has new files and directories. But you don’t want to lose the customers who remember the old domain and files. So, you should set up your mod_rewrite to redirect all the old files to the new domain:

RewriteRule ^.*$ http://newdomain.com/ [R=301,L]

Redirect the main domain to a sub-domain

RewriteEngine On

Options +FollowSymlinks

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]

RewriteRule ^ http://sub.domain.com [R,L]

Redirect a domain to sub-folder

RewriteEngine On

Options +FollowSymlinks

RewriteBase /

RewriteCond %{HTTP_HOST} www.domainname [OR]

RewriteCond %{HTTP_HOST} domainname

RewriteCond %{REQUEST_URI} !subfolder/

RewriteRule ^(.*)$ subfolder/$1 [L]

Redirect http to https


RewriteEngine on

RewriteCond %{HTTP_HOST} ^domain.com$ [NC]

RewriteCond %{SERVER_PORT} ^80$

RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]

non-WWW to WWW

RewriteEngine On  

RewriteCond %{HTTP_HOST} ^domain\.com [NC]

RewriteRule (.*) http://www.domain.com/$1 [L,R=301]

Category : Apache, Howtos, Troubleshooting

George K.

George K.

George started his career in web hosting and Linux technical support in the year 2004 and is with SupportSages since 2009. He has keen interest in server optimizations, custom security solutions, hacked server recovery, cyber forensic and high availability fail over system design and implementation. George loves long drives and is passionate about art and literature.

You may also read:

Comments

Add new commentSIGN IN

Let's Connect

Get new updates

Categories

$0.000 items