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

SSL / HTTPSApache Web Server

Avatar of undefined
Last Comment
Timothy Golden

8/22/2022 - Mon
David Favor

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.
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

David Favor

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
David Favor

Try removing the records I mentioned + see if you problem resolves.
ASKER CERTIFIED SOLUTION
Timothy Golden

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question