简体   繁体   中英

Apache redirect to https://www and then proxy to localhost

I've got a node.js app running on localhost:8180 .

I want the app to be accessible from the web only via https://www.example.com .

The goal is to redirect all traffic going to http://example.com , http://www.example.com , https://example.com to https://www.example.com .

I have access to the Plesk panel v12.5.30. In this panel I can configure separate .htaccess rules for http and https requests. The panel looks like this:

Plesk Apache directives panel

Can I achieve my goal using this panel?

I've tried the following rules for http and https but get a proxy error.

http

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com$1 [L,R=301]`

https

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://localhost:8180$1 [P]
ProxyPassReverse / http://localhost:8180/

I highly recommend using Nginx Directives Instead like this:

if ($scheme != https) {
    return 301 https://www.example.com$request_uri;
}

使用Nginx重定向301

You can also try a better compatibility htaccess code on top of all your .htaccess file codes:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM