3

I have a virtual server with one IP address, serving a few different sites. One of them has an SSL certificate on it.

I needed to add an SSL cert on a second domain for private use by myself, and as I only have one IP address, I added it on port 8080 instead. This works fine.

My problem now is that all the domains that point to the server will display my private site if port 8080 is requested on that domain.

https://example1.com:8080/ -> will show my private site, but shouldn't https://example2.com:8080/ -> will show my private site, but shouldn't https://desired-domain.com:8080/ -> will show my private site, this is correct!

Anyone visiting https://example1.com:8080/ should be redirected to http://example1.com/, and the same for example2.com.

I tried...

RewriteCond %{HTTP_HOST} !desired\-domain\.com [NC]

RewriteRule ^(.*)$ http://%{HTTP_HOST}$1 [L,R=301]

...but the port is appended to HTTP_HOST, so it comes out as http://example1.com:8080/

So, in short, my question: how can I redirect to the requested host, but ignore the request port?


EXTRA

I've tried this...

RewriteCond %{HTTP_HOST} !(desired-domain.com):8080$ [NC]

RewriteRule ^(.*)$ http://%1$1 [L,R=301]

...but it redirects to http://localhost/. That's better than showing the wrong site's page, though, as nobody should be requesting port 8080 on any other domain anyway!

I suspect there's a better 'desired-domain.com' regex, but my regex-mojo isn't flowing today.

JoLoCo
  • 1,355
  • 2
  • 13
  • 23
  • Also, I tried the tips here: http://stackoverflow.com/questions/6814745 but it did nothing for me! – JoLoCo Mar 08 '12 at 01:00
  • Maybe explicitly specifying port 80 works: `RewriteRule ^(.*)$ http://%{HTTP_HOST}:80$1 [L,R=301]` instead – Gerben Mar 08 '12 at 17:50
  • Thanks @Gerben, I tried that but it just gives ```http://example1.com:8080:80/```! – JoLoCo Mar 08 '12 at 19:47

1 Answers1

2

Maybe try:

RewriteCond %{HTTP_HOST} ^(example[12].com):8080$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

Obviously, you'll need to be a bit clever about your HTTP_HOST regular expression, if there's no easy way of encapsulating your example1 and example2 domains within a single regex, you can have 2 of then and use the [OR]:

RewriteCond %{HTTP_HOST} ^(example1.com):8080$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(example2.com):8080$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

EDIT:

You cannot match a backreference like !(desired-domain.com):8080$, as the ! means that it doesn't match. But you can try something like this:

RewriteCond %{HTTP_HOST} !^desired-domain.com(:8080)?$ [NC]
RewriteCond %{HTTP_HOST} ^(.*):8080$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thanks for your answer, but ```%1``` doesn't redirect to the domain name the user requested, it redirects to the domain name defined on the server - so it's the same as me specifying a domain in the RewriteRule. The RewriteCond is the easy bit, that works fine! – JoLoCo Mar 08 '12 at 01:36
  • The %1 is a backreference to a match in RewriteCond, so whatever matched in the previous RewriteCond's parens () can be referenced by %1 (or %2, %3, %4, etc) just like the $1 backreferences a match in the RewriteRule. – Jon Lin Mar 08 '12 at 04:17
  • in the second solution %1 will be empty if the http_host is not example.com. This is because %1 will only backreference the last rewritecond, not the last matching one. – Gerben Mar 08 '12 at 17:49
  • Hmm... let me try again now! The last time I tried ```%1```, it came out as ```localhost``` instead... – JoLoCo Mar 08 '12 at 19:47
  • @JonLin & @Gerben - thank you both for your helpful information, knowing what ```%1``` refers to certainly helps! I've added some more information to the bottom of my question. I might focus in on the RewriteCond regex and see if I can crack this one... – JoLoCo Mar 08 '12 at 19:59
  • @JoLoCo see my edit, you can't match a backreference that way – Jon Lin Mar 08 '12 at 22:42
  • @JonLin - Excellent, that works perfectly, I knew there'd be a solution somewhere! Thank you very much for your answer! (By the way, I removed the slash between ```%1``` and ```$1``` in the ```RewriteRule``` as it adds another if it's in there.) – JoLoCo Mar 09 '12 at 02:37
  • 1
    If this is a reg exp ^(example1.com):8080$ the dot there means every character. Maybe you want escape that? ^(example1\.com):8080$ – DrLightman Jan 26 '17 at 11:32