1

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?

3
  • Is your certificate multidomain or wildcard? I mean, does it have "example.com" literally? May 20, 2020 at 20:29
  • It's multidomain, not wildcard. It specifically lists example.com, anotherexample.com, differentexample.com. Browsers don't complain on any of those domains about security and do list those domains when you examine the cert.
    – Maltiriel
    May 20, 2020 at 20:33
  • can you show your current config for :80 and :443 example.com ? Not in comment, edit your question. May 20, 2020 at 21:20

2 Answers 2

2

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>
8
  • Excellent, thanks, that did the trick! Nice and simple, too.
    – Maltiriel
    May 20, 2020 at 21:38
  • Never prefer mod_rewrite over mod_alias! httpd.apache.org/docs/2.4/rewrite/avoid.html May 21, 2020 at 5:23
  • I'm afraid you're twenty years too late, @EsaJokinen. May 21, 2020 at 8:17
  • How? That's the current official documentation of the latest Apache HTTPd. It wouldn't be there if it was some legacy from the 90s, and it's also well reasoned. It's never too late to stop misguiding people. How would it ever stop, if everyone keeps spreading bad practices like using mod_rewrite for everything and placing configuration in .htaccess while it belongs to server/vhost configuration? These are from the shared web hosting era, when there might not have been better alternatives allowed. That's in the past. May 21, 2020 at 9:00
  • 1
    Well, I am the past, @EsaJokinen. Never once used a redirect, got nothing but praise from my customers for what I could realize with mod_rewrite. But I'm sure you will do much better. May 21, 2020 at 10:29
2

Gerard's answer preferring mod_rewrite over mod_alias leaves an illusion that this couldn't be achieved using mod_alias. According to Apache's official documentation:

When not to use mod_rewrite

mod_rewrite should be considered a last resort, when other alternatives are found wanting. Using it when there are simpler alternatives leads to configurations which are confusing, fragile, and hard to maintain. Understanding what other alternatives are available is a very important step towards mod_rewrite mastery.

Simple Redirection

mod_alias provides the Redirect and RedirectMatch directives, which provide a means to redirect one URL to another. This kind of simple redirection of one URL, or a class of URLs, to somewhere else, should be accomplished using these directives rather than RewriteRule. RedirectMatch allows you to include a regular expression in your redirection criteria, providing many of the benefits of using RewriteRule.

The only problem with your RedirectMatch 301 ^/$ /app1/ is that the last parameter is not an URL, but a relative reference.

RedirectMatch Directive

Syntax: RedirectMatch [status] regex URL

Full configuration using mod_alias would be e.g.:

<VirtualHost *:80>
    ServerName example.com

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

<VirtualHost *:443>
    ServerName example.com

    RedirectMatch 301 ^/$ https://example.com/app1/
</VirtualHost>
1
  • 1
    I did decide to use this method as I had seen answers on other questions stating that mod_alias was preferred. Thanks for posting the documentation pertaining to that. But I didn't need to use the full URL; it's working perfectly on my server now with the addition of RedirectMatch 301 ^/$ /app1/ to the 443 VirtualHost block.
    – Maltiriel
    May 21, 2020 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.