force ssl with htaccess and angular7

Asked

Viewed 144 times

1

Well I have a website hosted at UOL, and I’m going through a strange problem.

The structure of the site is so:

| index.html
| .htaccess
|
|app1
|    | .htaccess
|    |index.html
|
|app2
|    | .htaccess
|    |index.html

Good my file . Root htaccess is like this:

RewriteEngine On

# Redirecionar para HTTPS WC
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Already . htaccess file of the folder app1 and app2 is like this:

# Redireciona as requisições para index.html
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) index.html [NC,L]
</IfModule>

The problem is that when someone accesses app1 or app2 . htaccess from the root does not force the use of ssl.

To work I have to put a . htaccess inside each app the same rule of the root.

Have some way to force ssl through the Rais . htaccess?

  • As far as I know, apache inherits htaccess settings from the root folder to the subfolders... Maybe you have some configuration for this, but cmg always worked in the pattern.

3 answers

0

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Change the example domain to yours, and if you use another port other than 80, change it as well.

  • I tried that and it didn’t work.

0

Use the .htacess below, it will cause you to access any page of your site using http://seudominio.com, www.seudominio.com or seudominio.com will cause the URL to be converted to https://seudominio.com

Code .htacess:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Remembering that if your . htacess is already with RewriteEngine On don’t need to put it back.

  • I just tested it and it didn’t work out. I’m thinking it’s something wrong with Uol hosting.

0

So that’s it, as said in the comment, by putting a file .htaccess in the root folder of a website, it will act on all folders of this. But, a file .htaccess in a subfolder cancels the action of the previous one acting on that folder and its subfolders, the solution for you is to use this rule in each .htaccess present on your website, try to use the .htaccess in app1 and app2 as follows:

# Redireciona as requisições para index.html
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) index.html [NC,L]
</IfModule>

# INICIA REGRA SSL
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# FINAL REGRA SSL
  • 1

    So it only works if I put one .htaccess in each folder of the website.

  • Do you use any CMS? This .htaccess is at the root of Wordpress in my case, and I don’t own any more .htaccess , remember that if you use one per folder, the internal rules override those of the site root...

  • 1

    I think I found the problem, inside the subfolder I have an htaccess configured to run an application in angular, this is giving conflict. I’ll edit my question.

  • So that’s it, by putting a file .htaccess in the root folder of a website, it will act on all folders of this. But, a file .htaccess in a subfolder cancels the action of the previous one acting on that folder and its subfolders, the solution for you is to use this rule in each .htaccess present on your website.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.