-1

I have two domains old.example and new.example.

I want to redirect old.example to new.example sending a 301 response code and forcing to use https.

All works fine using HTTP, but it fails with HTTPS. i.e. if I enter http://old.example address in browser changes to https://new.example.

But if I enter https://old.example nothing changes and navigation proceeds using https://old.example.

Below there configuration files both for HTTP and HTTPS. Apache version is 2.2.15 (Unix)

Anyone can help me to understand the problem?

http.conf:

VirtualHost *:80>
   ServerName   192.0.2.11
   ServerAlias  old.example www.old.example new.example www.new.example

   RewriteEngine on
   RewriteLog "/tmp/rewrite.log"
   RewriteLogLevel 3
   RewriteCond %{HTTP_HOST} =old.example
   RewriteRule ^(.*)$ https://new.example$1 [R=permanent,L]

   SecRuleEngine Off

   ProxyRequests off
   ProxyPreserveHost On

   <Proxy *>
     Order deny,allow
     Allow from all
   </Proxy>

   ProxyPass / ajp://localhost:8009/
   ProxyPassReverse / ajp://localhost:8009/

   <Location />
     Order allow,deny
     Allow from all
   </Location>
</VirtualHost>

httpd-le-ssl.conf:

<IfModule mod_ssl.c>
<VirtualHost *:443>
   ServerName   192.0.2.11
   ServerAlias  old.example www.old.example new.example www.new.example

   SSLEngine on
   RewriteEngine on
   RewriteLog "/tmp/rewrite-ssl.log"
   RewriteLogLevel 3
   RewriteCond %{HTTPS} =on
   RewriteCond %{HTTP_HOST} =old.example
   RewriteRule ^(.*)$ https://new.example$1 [R=permanent,L]


   SecRuleEngine Off

   ProxyRequests off
   ProxyPreserveHost On

   <Proxy *>
     Order deny,allow
     Allow from all
   </Proxy>

   ProxyPass / ajp://localhost:8009/
   ProxyPassReverse / ajp://localhost:8009/

   <Location />
     Order allow,deny
     Allow from all
   </Location>

   Include /etc/letsencrypt/options-ssl-apache.conf
   SSLCertificateFile /etc/letsencrypt/live/old.example/cert.pem
   SSLCertificateKeyFile /etc/letsencrypt/live/old.example/privkey.pem
   SSLCertificateChainFile /etc/letsencrypt/live/old.example/chain.pem

  • 1
    Apache 2.2 is very EOL. You should upgrade to v. 2.4, and, after you do, you will also find that what you want to accomplish is actually much easier and, potentially, less resource consuming. – Colt Jun 9 at 22:16
  • In any case, for the same reason, this question is off-topic on Server Fault. – Colt Jun 9 at 22:17
  • @Colt I agree with you that is an old version, but it has all features to reach the goal we need. – fciri Jun 9 at 22:27
  • 3
    While Apache 2.2 may have all of the features that you want, what it does not have is any evaluation, let alone patch, for any of the twenty-six security vulnerabilities that have been addressed in Apache 2.4 in the time since version 2.2 went EOL. – Colt Jun 10 at 2:51
2
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} =old.example
RewriteRule ^(.*)$ https://new.example$1 [R=permanent,L]

It's not clear why these directives would not redirect https://old.example. However, it's not strictly correct and can be immediately simplified. For instance, it certainly won't redirect www.old.example as you are explicitly checking for old.example (an exact match) only (this also applies to the port 80 vHost). You should check that it is not new.example (the canonical hostname) instead.

Checking the HTTPS server variable inside the host for port 443 is entirely superfluous.

So, this can be rewritten as:

RewriteCond %{HTTP_HOST} !=new.example
RewriteRule ^(.*)$ https://new.example$1 [R=permanent,L]

However, you should consider splitting your old-domain into a separate vHost and implementing a simple mod_alias Redirect instead of using mod_rewrite here.

1

Thanks all for the help.

The problem was that section below was duplicated in another files.

<VirtualHost *:443>

</VirtualHost>

However I accepted @MrWhite solution because he suggested two improvement in configuration: splitting each domain in a separate vHost and to remove HTTPS server variable.

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.