0

I am trying to do the following with Apache:

If I ask for http(s)://domain.local(/) I should be redirected to a completely new domain like this https://www.domain_new.local

If I ask for http://domain.local/gp I should be redirected to https://domain.local/gp

I tried the following, but doesn't seem to work:

<VirtualHost *:80>
    ServerName domain.local

    DocumentRoot "/var/www/html/"
    
    # html directory contains gp directory 
    <Directory /var/www/html>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    RewriteEngine on

    RewriteCond "%{HTTP_HOST}%{REQUEST_URI}" "^domain\.local\/?$"
    RewriteRule "^domain\.local\/?$" "https://domain_new.local"

    RewriteCond "%{HTTP_HOST}%{REQUEST_URI}" "^domain\.local\/gp(.*)"
    RewriteRule "^/?(.*)" "https://%{SERVER_NAME}/$1"
</VirtualHost>


<VirtualHost *:443>
    ServerName domain.local

    DocumentRoot "/var/www/html/" 
    
    SSLEngine on
    
    # html directory contains gp directory     
    <Directory /var/www/html>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    SSLCertificateFile /etc/pki/tls/certs/domain.local.crt
    SSLCertificateKeyFile /etc/pki/tls/private/domain.local.key
    SSLCertificateChainFile /etc/pki/tls/certs/domain.local.ca-bundle
</VirtualHost>
1
  • plz help on this
    – elekgeek
    Jul 11, 2020 at 13:38

2 Answers 2

1

I did not test it but I notice several (potential) problems:

  • RewriteCond "%{HTTP_HOST}%{REQUEST_URI}" "^domain\.local\/?$"
    The \ before the / may be a problem. In general there is no reason to involve REQUEST_URI in RewriteCond as you have it in the RewriteRule line again.
  • RewriteRule "^domain\.local\/?$"
    The domain does not belong there. RewriteRule matches the REQUEST_URI only.
  • The rewrite rule for https://domain.local is missing in the HTTPS configuration.
5
  • thank you for your answer, how can I catch that the URL contains only domain.local and rewrite it. This is the problem I am currently facing to be honest. I think I only need a rule that redirects if URL only equals domain.local.
    – elekgeek
    Jul 12, 2020 at 8:27
  • @elekgeek RewriteCond "%{HTTP_HOST}" "^domain\.local$" As the problem is solved you should accept my answer. Jul 12, 2020 at 12:26
  • %{HTTP_HOST} alone doesn't work, you must include %{REQUEST_URI} because you won't be able to find out if the user wants gp or not, read my answer again.
    – elekgeek
    Jul 12, 2020 at 16:16
  • @elekgeek As I already pointed out in my answer: There is no need for REQUEST_URI in the RewriteCond line because that value appears (and is checked) in the RewriteRule line anyway. It is possible to handle this as you do, just not useful. Jul 12, 2020 at 16:20
  • You are wrong, without %{REQUEST_URI} you will not be able to detect if the user wrote domain.local/gp unless you use %{REQUEST_URI} where %{REQUEST_URI} is equal to gp/, also without \/ RewriteCond will always evaluate to false. Your answer is partially correct, anyone who facing similar issue will look for a more comprehensive answer to be honest, this is why I created this answer, so that anyone can benefit from it. I am starting to feel you are thinking the exact opposite of what I asked in the original question.
    – elekgeek
    Jul 12, 2020 at 17:06
0

The solution is much simpler that I was thinking. You simply redirect all HTTP to HTTPS and then handle the actual rewrite in 443 virtual host, thanks to @Hauke Laging who pointed out that actual redirection should occur in 443 virtual host and also errors in the RegEx I was using. This article How to force https, www and a trailing slash with one redirect is interesting read as well.

<VirtualHost *:80>
    ServerName domain.local

    DocumentRoot "/var/www/html/"
    
    # html directory contains gp directory 
    <Directory /var/www/html>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>


<VirtualHost *:443>
    ServerName domain.local

    DocumentRoot "/var/www/html/"

    RewriteEngine on
    RewriteCond "%{HTTP_HOST}%{REQUEST_URI}" "^domain\.local\/?$"
    RewriteRule .* https://domain_new.local
    
    SSLEngine on
    
    # html directory contains gp directory     
    <Directory /var/www/html>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    SSLCertificateFile /etc/pki/tls/certs/domain.local.crt
    SSLCertificateKeyFile /etc/pki/tls/private/domain.local.key
    SSLCertificateChainFile /etc/pki/tls/certs/domain.local.ca-bundle
</VirtualHost>

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.