1

Whenever someone visits intern.old-company-name.example.com I would like it to get redirected to https://intern.new-company-name.example.com. One way to do it would be

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*) https://intern.new-company-name.example.com$1
#    RewriteRule ^(.*) https://%{HTTP_HOST}$1                                                             
</IfModule>

but I don't like that I hardcode the outer sub domain intern into the httpd.conf.

Question

Would it be possible to do a regex on %{HTTP_HOST} so old-company-name gets replaced with new-company-name before the redirection to https? Or perhaps something similar?

Update

# apachectl -S
VirtualHost configuration:
10.10.10.10:443     is a NameVirtualHost
         default server a.y.b.com (/etc/httpd/conf.d/ssl.conf:85)
         port 443 namevhost a.y.b.com (/etc/httpd/conf.d/ssl.conf:85)
Syntax OK
Jasmine Lognnes
  • 2,490
  • 8
  • 31
  • 51
  • @ETL From what I can gather the link to point to does not explain how to modify the sub domain part. – Jasmine Lognnes Feb 14 '14 at 18:03
  • Can we assume that your rewrite rules have `[R]` at the end? The https rewrite won't create an SSL request any other way and it changes the meaning of the other rewrite rule significantly. – Ladadadada Feb 14 '14 at 18:55
  • By the way, it's better to tag your questions with just the tags that are relevant. Tagging with both [tag:ubuntu] and [tag:centos] is confusing and nothing about this question seems to be [tag:linux] specific. A [tag:mod-rewrite] tag might have been more appropriate. – Ladadadada Feb 14 '14 at 18:59
  • @JasmineLognnes This is doable, but can you give us more of an idea of what the expected inputs to be? Is it always one subdomain on top of the same parent domain, or are there other scenarios to account for? – Shane Madden Feb 14 '14 at 19:27
  • @Ladadadada `[R]` flag is fine. The rule I posted were just my current solution, which I would like to make more generic. I.e. without hardcoding the `intern` sub domain. Hard coding `new-company-name.example.com` is fine. – Jasmine Lognnes Feb 15 '14 at 10:49
  • @ShaneMadden Yes, the task is to redirect `a.x.b.com` to `https://a.y.b.com` where hard coding `y.b.com` is fine. I would like to avoid `a` being hard coded. – Jasmine Lognnes Feb 15 '14 at 10:56

2 Answers2

2

Let's have the HTTP to HTTPS behavior handled by the HTTP listener, and the old name to new name behavior handled by the HTTPS listener. (We could have the HTTP redirect also do the name change, but this keeps everything in one place and makes it simpler.)

So, in your main config file we'll put back your original config with a slight tweak:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://${HTTP_HOST}$1

Then inside the <VirtualHost> in /etc/httpd/conf.d/ssl.conf we'll do the name redirect:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.old-company-name\.example\.com$
RewriteRule ^(.*)$ https://%1.new-company-name.example.com$1 [R,L]
Shane Madden
  • 112,982
  • 12
  • 174
  • 248
  • Why is `[R]` needed? Is `L` for security reasons? – Jasmine Lognnes Feb 16 '14 at 14:18
  • 1
    @JasmineLognnes The `R` is implied by the fact that the destination of the rewrite is not the current virtualhost, and the `L` is to ensure that no other `RewriteRule` configuration somewhere else (say, in an `.htaccess` file) is able to fiddle with the redirect before it's sent. – Shane Madden Feb 17 '14 at 00:40
  • If I visit `httpS://intern.old-company-name.example.com` then it stays on `old-company-name` instead of redirecting to `httpS://intern.new-company-name.example.com` – Jasmine Lognnes Feb 17 '14 at 11:31
  • @JasmineLognnes Where are these rules located (virtual host? htaccess file?) - and is that `RewriteCond %{HTTPS} off` still in place? – Shane Madden Feb 17 '14 at 17:58
  • No virtual hosts. I placed it as the bottom of `httpd.conf`, and I still have `RewriteCond %{HTTPS} off` but if needed it can be changed. – Jasmine Lognnes Feb 18 '14 at 09:47
  • @JasmineLognnes Yeah, remove that - that condition is explicitly preventing the following rule from applying for https requests. – Shane Madden Feb 18 '14 at 20:06
  • Even if I do that, then `httpS://a.x.b.com` is for some reason still not redirected to `httpS://a.y.b.com`. The 3 other cases works perfectly. – Jasmine Lognnes Feb 19 '14 at 14:54
  • 1
    @JasmineLognnes Actually, just noticed your edit to the answer. Instead of using this same rule to point from HTTP to HTTPS, we'll need to split them up - one rule for the name correction, and one for the HTTPS redirect. If that rule were being applied to the `https://a.y.b.com` requests, then the condition would still match and it'd be a redirect loop - so what that tells us is this rule isn't applying to the HTTPS requests at all. Can you check the output of `apachectl -S`? There should be a `` somewhere since there are both http and https listeners configured. – Shane Madden Feb 19 '14 at 16:37
  • Hmm. That sounds very interesting! Updated OP. – Jasmine Lognnes Feb 19 '14 at 18:30
  • @JasmineLognnes Edited answer with a different approach that should work in this case. – Shane Madden Feb 19 '14 at 19:39
  • With the new approach both http and https on the old domain does not get redirected, but stays on the domain. – Jasmine Lognnes Feb 20 '14 at 08:26
  • @JasmineLognnes Strange. Is it redirecting to HTTPS? – Shane Madden Feb 20 '14 at 17:45
  • Yes, `http://old...` -> `httpS://old...`, but not afterwards to `httpS://new...`. – Jasmine Lognnes Feb 21 '14 at 16:05
  • @JasmineLognnes Are the rules inside the `` in `ssl.conf`? And I forgot to mention you'll need a `RewriteEngine On` in that file if there isn't one already, can you check on that? – Shane Madden Feb 21 '14 at 20:31
0

Modifying headers can be done with mod_headers - check http://httpd.apache.org/docs/2.2/mod/mod_headers.html for all the syntax and details. I can't say for sure you can acheive what you want - and I'm not sure really why you want to modify the header in this case.

ETL
  • 6,443
  • 1
  • 26
  • 47