apache rewrite rules old-dmain to new-domian

Timothy Golden
Timothy Golden used Ask the Experts™
on
apache rewrite rules to redirect http(s) olddomian.com to newdomain.com
i made a custom  VirtualHost .conf file for apache for my old domian to redirect every request to httpS new domain but it isn't working...?
i get either the old host or some an invalid URL... both FireFox and Chome show errors like this:
The owner of OldDomian.com has configured their website improperly. To protect your information from being stolen, Firefox has not connected to this website. The certificate is only valid for NewDomain.com.

a copy of the apache.conf VirtualHost config:
<VirtualHost 129.125.125.128:80>
ServerName  OldSiteHost.com
ServerAlias www.OldSiteHost.com
ServerAlias OldSiteHost.com

RewriteEngine on
RewriteCond %{SERVER_NAME} =OldSiteHost.com [OR]
RewriteCond %{SERVER_NAME} =www.newcorp.OldSiteHost.com [OR]
RewriteCond %{SERVER_NAME} =newcorp.OldSiteHost.com [OR]
RewriteCond %{SERVER_NAME} =www.OldSiteHost.com
RewriteRule ^ https://NewSiteHost.com/ [END,NE,R=permanent]

</VirtualHost>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Likely better to take a completely different approach.

You'll use 2x config files. One will be for new site. One will be for old site.

Note: Do not use R=301/permanent use R=302 instead.

Using a permanent redirect will cause far more problems than you imagine.

Always use a 302 (temp redirect) to avoid complex browser caching problems for which there is no recovery. If you think you must use a 301, then only change 302 -> 301 after you've thoroughly tested your redirects.

Config file setups...

1) Your new site config will never mention your old site.

2) Your old site config will have exactly one redirect, from the terminal/final site URL to your new site URL.

Author

Commented:
i do have 2 separate config files one for each virtual host below are the sample configs

Old Site

<VirtualHost 129.125.125.128:80>
ServerName  OldSiteHost.com
ServerAlias www.OldSiteHost.com
ServerAlias OldSiteHost.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =OldSiteHost.com [OR]
RewriteCond %{SERVER_NAME} =www.new.OldSiteHost.com [OR]
RewriteCond %{SERVER_NAME} =new.OldSiteHost.com [OR]
RewriteCond %{SERVER_NAME} =www.OldSiteHost.com
RewriteRule ^ https://NewSiteHost.com/ [END,NE,R=302]
</VirtualHost>

Open in new window

NEW Site

<VirtualHost 129.125.125.128:80>
ServerName  NewSiteHost.com
ServerAlias www.NewSiteHost.com
ServerAlias NewSiteHost.com
bla bla bla
</VirtualHost>

Open in new window

David FavorFractional CTO
Distinguished Expert 2018

Commented:
In old site file... remove line #4 + #7 + #8.

In new site file... remove line #4.

Having a ServerName + ServerAlias both match likely results in undefined behavior.

Might work correctly now.

Might work far differently in the future.

Likely this will be an error in the future, because...

ServerName  NewSiteHost.com - this means serve content.

ServerAlias NewSiteHost.com - this means to return a 301 for NewSiteHost.com which is an error or a conflict with the ServerName directive.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Try removing the records I mentioned + see if you problem resolves.
Commented:
i deleted the old site config, and recreated it and now its working

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial