0

everyone.

My problem is simple, but I cannot manage to find a solution.

All I need to do is use RewriteRule on Apache to redirect all request from a hostname to its FQDN.

For example, if the request is https://hostname/test.html, I want Apache to redirect the request to https://hostname.test.com/test.html. This has to be rewritten to any request to that hostname.

How can I do this?

Edit 1:

I've noticed that the following RewriteRule works for HTTP, but not for HTTPS:

RewriteCond %{​​​​​​​HTTP_HOST}​​​​​​​ ^hostname$
RewriteRule ^(.*)$ https://hostname.test.com%{​​​​​​​​REQUEST_URI}​​​​​​​​ [R,L]

What am I missing?

BR

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Can you please share what you have tried so far? – Amit Verma Mar 30 '21 at 11:09
  • Hi, @AmitVerma. I've tried setting ServerName to hostname.test.com and ServerAlias to hostname. Then I tried: RewriteEngine On RewriteCond %{HTTP_HOST} hostname [NC] RewriteRule ^(.*)$ https://hostname.test.com/$1 [R=302] – tomasaazevedo Mar 30 '21 at 12:45
  • @tomasaazevedo, Your shown code looks to be fine to me. Could you please confirm for which sample https URL its not working? – RavinderSingh13 Mar 30 '21 at 13:44

1 Answers1

0

I've managed to find the problem.

The redirect is working.

The problem is that the certificate does not contain the hostname version of the URL, only the FQDN.

Since the certificate is checked before the redirect, hence the problem.

Sorry for the mess up