0

I am attempting to redirect

<VirtualHost *:5555>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{SERVER_PORT} =5555
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

However when I attempt to connect to via a web browser it is adding an extra / inbetween the %{HTTP_HOST}//%{REQUEST_URI}

How do I remove the extra / between the values?

MrWhite
  • 13,315

3 Answers3

1

If the Redirect directive is used within a <Location> or <LocationMatch> section with the URL-path omitted, then the URL parameter will be interpreted using expression syntax.

This syntax is available in Apache 2.4.19 and later

This makes this, once again, a case of When not to use mod_rewrite.

<VirtualHost *:5555>
    <Location />
        Redirect permanent https://%{HTTP_HOST}/
    </Location>
</VirtualHost>
Esa Jokinen
  • 52,963
  • 3
  • 95
  • 151
0

No need to check the port or if it is SSL, if a requests that is SSL lands here you will get an error since virtualhost won't be able to answer, and virtualhost is already specifying it is port 5555 the port this virtualhost is answering too, so you those redundand directives are not necessary.

If you insist on using mod rewrite and using variables this would be the most simple/correct way.

<VirtualHost *:5555>
    ServerName normallyouwantahostnamehere.example.com
    RewriteEngine On
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R=301,L]
</VirtualHost>

If you know the hostname I would be inclined to define it correctly and use a simple redirect (which redirects everything without the need to capture):

<VirtualHost *:5555>
    ServerName hostname.example.com
    Redirect / https://hostname.example.com/
</VirtualHost
-2

It looks like you're trying to redirect HTTP requests on port 5555 to HTTPS without an extra slash between the %{HTTP_HOST} and %{REQUEST_URI}. You can achieve this by modifying your RewriteRule slightly.

Here's the updated configuration:

<VirtualHost *:5555>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{SERVER_PORT} =5555
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

The key change here is in the RewriteRule. By using ^ before https://%{HTTP_HOST}, it anchors the rule to the beginning of the URL, ensuring that there won't be an extra slash inserted.

With this modification, the redirect should work without the extra slash between %{HTTP_HOST} and %{REQUEST_URI}.

Let us know if this works, or if you're still having problems!