大网数据广告活动
服务器运维问答

How can I configure Apache to redirect root to a suburi in addition to https redirection?

· 52CE 我爱测

I have an Apache webserver running a bunch of web apps. I have successfully redirected incoming http traffic to https for every individual app, but I'm having trouble routing ALL traffic coming to the root path (which has nothing…

问题原文

原帖链接 · Maltiriel · CC BY-SA 4.0

I have an Apache webserver running a bunch of web apps. I have successfully redirected incoming http traffic to https for every individual app, but I'm having trouble routing ALL traffic coming to the root path (which has nothing on it) to a specific app. I've got it working for http but not https.

So essentially right now the following URLS redirect correctly:

http://example.com/app1 -> https://example.com/app1
http://example.com/app2 -> https://example.com/app2 etc.
http://example.com -> https://example.com/app1

but I can't figure out how to make this work:

https://example.com -> https://example.com/app1

My Apache config file contains the following:

<VirtualHost xxx.xxx.xxx.xx:80>
  ServerName example.com

  RedirectMatch 301 ^/$ /app1/
  Redirect permanent / https://example.com/
</VirtualHost>

I have tried adding RewriteCond/RewriteRule pairs such as

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^/$ https://example.com/app1 [R=301,L]

as well as loads of other things that I thought ought to work and they either seem to do nothing or break other parts of my configuration.

In case it matters my SSL cert is multidomain as I also have other domains pointing to apps on this server. All of those work perfectly fine with just the following (they have no additional redirect requirements though):

<VirtualHost xxx.xxx.xxx.xx:80>
  ServerName example2.com

  Redirect permanent / https://example2.com/
</VirtualHost>

So how can I make https redirect from root to a suburi without breaking anything else?

已采纳解答

原帖链接 · Gerard H. Pille · CC BY-SA 4.0

The same RewriteRule for http and https should do the trick, put them first if there are others. I prefer mod_rewrite over mod_alias.

<VirtualHost xxx.xxx.xxx.xx:80>
    ServerName example.com

    RewriteEngine On
    RewriteRule ^/$ https://example.com/app1 [R=301,L]
</VirtualHost>

<VirtualHost xxx.xxx.xxx.xx:443>
    ServerName example.com

    RewriteEngine On
    RewriteRule ^/$ https://example.com/app1 [R=301,L]
</VirtualHost>

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-05-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。