Link to home
Avatar of Timothy Golden
Timothy GoldenFlag for United States of America

asked on

apache rewrite rules old-dmain to new-domian

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

Avatar of David Favor
David Favor
Flag of United States of America image

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.
Avatar of Timothy Golden

ASKER

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

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.
Try removing the records I mentioned + see if you problem resolves.
ASKER CERTIFIED SOLUTION
Avatar of Timothy Golden
Timothy Golden
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial