如何在 Apache httpd 中将特定 URL 路由到 http 和 https 以及其他 URL 到 https

Posted

技术标签:

【中文标题】如何在 Apache httpd 中将特定 URL 路由到 http 和 https 以及其他 URL 到 https【英文标题】:How to route a particular URL to both http and https and others to https in Apache httpd 【发布时间】:2014-11-06 08:35:00 【问题描述】:

我需要通过http and https 和其他网址(如/aapp, /bapp, /capp)和其他网址通过https 路由特定的网址,/gapp。我已成功将所有内容路由到 https,但无法将 /gapp 路由到 http。以下是我的配置:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %HTTPS =on
RewriteRule ^(gapp)$ http://%SERVER_NAME%REQUEST_URI [L,R]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %REQUEST_METHOD ^TRACE
RewriteRule .* - [F]
</IfModule> 

# Should mod_jk send SSL information to Tomcat (default is On)
JkExtractSSL On

# What is the indicator for SSL (default is HTTPS)
JkHTTPSIndicator HTTPS

# What is the indicator for SSL session (default is SSL_SESSION_ID)
JkSESSIONIndicator SSL_SESSION_ID

# What is the indicator for client SSL cipher suit (default is SSL_CIPHER)
JkCIPHERIndicator SSL_CIPHER

# What is the indicator for the client SSL certificated (default is SSL_CLIENT_CERT)
JkCERTSIndicator SSL_CLIENT_CERT

# Send everything for context /examples to worker named worker1 (ajp13)
# JkOptions indicates to send SSK KEY SIZE
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat
JkRequestLogFormat "%w %V %T"

JkMount /aapp aapp 
JkMount /bapp/* bapp

JkMount /capp capp 

JkMount /gapp gapp 
JkMount /gapp/* gapp 

简单来说,我希望能够做到这些:

http://example.com/gapp/
https://example.com/gapp/

我该如何做到这一点?

【问题讨论】:

【参考方案1】:

我相信您正在寻找与此相同的答案:.htaccess - htaccess redirect 4 specific pages

如果您想访问除 https 和 http 上的几个 url 之外的所有内容,您最好不要重定向,但只需为 URL 设置一个重写条件,如果它们匹配,则将它们重定向到 https:

#force https for certain pages
RewriteCond %REQUEST_URI !^/gapp
RewriteCond %HTTPS !=on
RewriteRule (.*) https://%HTTP_HOST%REQUEST_URI [L,R]

【讨论】:

我会试试这个并回复你

以上是关于如何在 Apache httpd 中将特定 URL 路由到 http 和 https 以及其他 URL 到 https的主要内容,如果未能解决你的问题,请参考以下文章

请教如何禁用apache的url encode

如何在 Apache 中将部分 URL 传递给 ProxyPass

根据 URL 路径或 apache(mod_ssl) 中的特定服务设置 SSL/TLS 协议版本

在 Mac OSX 10.6 中将新行附加到 etc/hosts 和 Apache httpd-vhosts.conf 的 Shell 脚本

如何配置Apache HTTP Server

apache开启路由重写功能