Redirect to a specified port when using https

0

1

I have an internal domain: cloud.flint.lo

I would like to achieve the following:

If URL=http://cloud.filnt.lo
   Redirect to: https://cloud.flint.lo:5001/sub
If URL=https://cloud.flint.lo
   Redirect to: https://cloud.flint.lo:5001

I wrote this in .htaccess, but only the first scenario works:

RewriteEngine On
RewriteCond %{SERVER_PORT} !^5001$
RewriteRule ^(.*)$ https://%{HTTP_HOST}:5001/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$                                                                                                                                                
RewriteRule ^(.*)$ https://%{SERVER_NAME}:5001/sub [L,R=301]

I also tried to use "REQUEST_URI" to match the 5001 port but didn't work either.

RewriteCond %{HTTPS} on                                                                                                                                                     
RewriteCond %{REQUEST_URI} !^.*\:5001
RewriteRule ^(.*)$ https://%{SERVER_NAME}:5001/sub [L,R=301]

NeilWang

Posted 2019-12-03T22:53:21.270

Reputation: 123

Answers

1

Using mod_rewrite seems a bit like overkill here. You may want to consider using Redirect (part of mod_alias) instead.

ex. Virtual Host 1 (HTTP)

<VirtualHost *:80>

ServerName cloud.flint.lo

Redirect permanent / https://cloud.flint.lo:5001/sub/

</VirtualHost>

ex. Virtual Host 2 (HTTPS)

<VirtualHost *:443>

ServerName cloud.flint.lo

Redirect permanent / https://cloud.flint.lo:5001/

</VirtualHost>

Notes

  • There may be some additional standard SSL statements required in any HTTPS virtual host (but this depends entirely on your configuration).

  • The example Redirect statements can of course simply be part of your primary Apache configuration files (particularly httpd-ssl.conf for the second example).

  • Redirect is used in place of DocumentRoot.

Using Apache mod_rewrite

Alternately, if you feel Apache mod_rewrite is more desirable for your use case, you can try the following:

ex. HTTP Host

RewriteEngine On
RewriteCond %{SERVER_PORT} ^80$                                                                                                                                                
RewriteRule ^(.*)$ {HTTP_HOST}:5001/sub$1 [L,R=301]

ex. HTTPS Host

RewriteEngine On
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}:5001$1 [L,R=301]

Anaksunaman

Posted 2019-12-03T22:53:21.270

Reputation: 9 278

The second example didn't work either. And I am still prefer the mod_rewrite method because the website may move to webhosting in the future. – NeilWang – 2019-12-16T01:53:36.620

I have added mod_rewrite options. However, please note that all the suggestion given should work. If the added examples do not work for you, there may be additional issues you are facing. – Anaksunaman – 2019-12-16T05:38:58.393

You may also wish give more specifics about the problem(s) you are encountering. – Anaksunaman – 2019-12-16T05:53:20.013

Both of the methods works now. Thank you @Anaksunaman! – NeilWang – 2019-12-18T00:29:27.963

Glad to hear it. You're welcome. =) – Anaksunaman – 2019-12-18T00:30:43.420