Apache:当网站在 HTTPS 上运行时通过 HTTP 提供资源

发布于 2025-01-05 17:20:04 字数 748 浏览 0 评论 0原文

我们的网站在 HTTPS 上运行。有没有办法接受包含 /public/ 的 URL 的 HTTP 请求?对于所有其他 HTTP 请求,应将它们重定向到 HTTPS。

我有在 apache + passenger 上运行的 RoR 应用程序。

编辑

由于资产(/public/)请求将显式位于HTTP上,因此如何创建另一个VHOST来处理 HTTP 请求。对于除 /public/ 之外的任何请求,都可以直接重定向到 HTTPS 吗?如果我们可以这样做,我们如何在 VHOST 中针对 HTTP 进行设置?

编辑2

抱歉,我应该首先详细说明这一点。这是我们的设置。有两个单独的应用程序。一个在 HTTPS (S) 上运行,另一个在 HTTP (P) 上运行。应用程序PS获取数据(一个完整的HTML页面,称之为页面)并呈现给客户端。 page 中使用的 CSS 文件位于“S”上,因此我需要在 CSS 链接中使用 HTTPS。我想使用 HTTP 来引用 CSS。

We have our site running on HTTPS. Is there a way to accept HTTP request for URLs containing /public/? For all other HTTP requests they should be redirected to HTTPS.

I have RoR application running on apache + passenger.

EDIT

Since the assets (/public/) requests will explicitly be on HTTP, how about creating another VHOST to handle HTTP requests. And for any requests other than /public/ directly could be redirected to HTTPS? If we can go this way how can we set this up in VHOST for HTTP?

EDIT 2

I am sorry, I should have been elaborated this in first place. Here is our setup. There two separate applications. One is running on HTTPS (S) and other on HTTP (P). The app P fetches data (a full HTML page, call it page) from S and render to client. The CSS file used in page is located on 'S' so I need to HTTPS in CSS link. I want to use HTTP instead to refer the CSS.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇俏 2025-01-12 17:20:04

您可以使用 mod_rewrite 并放置 < DocumentRoot 中包含以下内容的 code>.htaccess 文件。

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

Rewritecond %{REQUEST_URI} .*/public/.* [NC]
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Rewritecond %{REQUEST_URI} !.*/public/.* [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

NameVirtualHost *:80

Listen 80
Listen 443

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /pathto/DocumentRoot
ServerName domain.com

ErrorLog path/to/your-error_log
CustomLog path/to/your-access_log common

RewriteEngine on
RewriteBase /

Rewritecond %{REQUEST_URI} !.*/public/.*$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</VirtualHost>


<VirtualHost *:443>
ServerAdmin [email protected]
DocumentRoot /pathto/DocumentRoot
ServerName domain.com

ErrorLog path/to/your-error_log
CustomLog path/to/your-access_log common

RewriteEngine on
RewriteBase /
Rewritecond %{REQUEST_URI} .*/public/.*$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#All the other directives pertaining to SSL add below

</VirtualHost>

You can use mod_rewrite and place a .htaccess file with the below contents in your DocumentRoot.

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

Rewritecond %{REQUEST_URI} .*/public/.* [NC]
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Rewritecond %{REQUEST_URI} !.*/public/.* [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

NameVirtualHost *:80

Listen 80
Listen 443

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /pathto/DocumentRoot
ServerName domain.com

ErrorLog path/to/your-error_log
CustomLog path/to/your-access_log common

RewriteEngine on
RewriteBase /

Rewritecond %{REQUEST_URI} !.*/public/.*$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</VirtualHost>


<VirtualHost *:443>
ServerAdmin [email protected]
DocumentRoot /pathto/DocumentRoot
ServerName domain.com

ErrorLog path/to/your-error_log
CustomLog path/to/your-access_log common

RewriteEngine on
RewriteBase /
Rewritecond %{REQUEST_URI} .*/public/.*$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#All the other directives pertaining to SSL add below

</VirtualHost>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文