apache – How to redirect to a folder and HTTPS at the same time?


I have a site http://meusite.pt which is a CNAME of http://minhamaquina.amazon.com , where the site is hosted. But I needed it to be redirected to my amazon machine's meusite folder, that is, http://minhamaquina.amazon.com/meusite , but only http://mysite.pt .

I got it with this answer: https://stackoverflow.com/a/36972084

RewriteEngine on 

#RewriteCond %{HTTPS} !=on
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{HTTP_HOST} ^(www.)?meusite.pt$ 
RewriteCond %{REQUEST_URI} !^/meusite/ 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)$ /meusite/$1 

RewriteCond %{HTTP_HOST} ^(www.)?meusite.pt$ 
RewriteRule ^(/)?$ meusite/ [L]

My problem now is that I also need to redirect from http to https, that is, if I type http://mysite.pt it will redirect to https://mysite.pt which will have to fetch the content from http://minhamaquina .amazon.com/mysite .

The certificate is on meusite.pt .

Is it possible to do this with .htaccess ? Like?


I added the lines (commented) of the question that was indicated as a duplicate, but is looped:

This webpage has a redirect loop


I found a way to do it in SOen that solved my problem, it's not really an answer to what I asked, but it's the solution to the problem, which is basically using the lines suggested to me in the comments in an amazon configuration file as we can see in this answer .

In summary, just place a .config file in the project's .ebextensions directory :

        mode: "000644"
        owner: root
        group: root
        content: |
            RewriteEngine On
            <If "-n '%{HTTP:X-Forwarded-Proto}' && %{HTTP:X-Forwarded-Proto} != 'https'">
            RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

That when deploying the application adds this rule to the Elastic Beanstalk Apache.

Scroll to Top