5

I just set up HTTPS on my server, and I have an issue with redirect permanent.

Example: http://example.com/index.html redirects me to http://www.example.comindex.html.

The / (tail ending slash) is missing and I can't figure out how to fix it.

It works with http://www.example.com/index.html.

Here is my httpd.conf

<VirtualHost *:80>
  ServerName example.com
  Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:80>
  ServerName www.domain.com
  Redirect permanent / https://www.example.com/
</VirtualHost>

<VirtualHost *:443>
  DocumentRoot /var/www/example/
  ServerName www.example.com
  SSLEngine on
  SSLCertificateFile ssl.crt
  SSLCertificateKeyFile ssl.key
</VirtualHost>
1
  • 3
    Odd -- should be working just fine. Try RedirectMatch instead: RedirectMatch 301 ^(.*)$ https://www.domain.com$1 -- see if it makes the difference. Also -- modern browsers do cache permanent redirects, so if it failed first time, then browser may still be using old redirect. I suggest clear browser cache and restart it (and/or try another browser).
    – LazyOne
    Apr 24 '12 at 13:10
2

Change to this and try it, notice only two VirtualHost

<VirtualHost *:80>
  ServerName example.com
  ServerAlias *.example.com
  Redirect permanent / https://www.example.com/
</VirtualHost>

<VirtualHost *:443>
  DocumentRoot /var/www/example/
  ServerName www.example.com
  SSLEngine on
  SSLCertificateFile ssl.crt
  SSLCertificateKeyFile ssl.key
</VirtualHost>
1

I had that same issue, and don't know why it is failing either. I was able to work around it using this instead:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
#   Redirect 301 / https://www.example.com
    RedirectMatch permanent /(.*) https://www.example.com/$1
</VirtualHost>
1
  • The (commented out) Redirect directive is missing a trailing slash on the end of the target URL - this will result in a missing slash in the redirected URL.
    – MrWhite
    Aug 24 '20 at 17:00
0

Example: http://example.com/index.html redirects me to http://www.example.comindex.html.
The / (tail ending slash) is missing and I can't figure out how to fix it.

This will happen if you are missing the trailing slash from the end of the target URL-path on the Redirect directive, when the slash is included on the source URL. For example: Redirect / https://www.example.com or Redirect /subdir/ https://www.example.com/subdir - the slashes need to match.

However, this is not the case with the directives as posted in the question, since you appear to be including the trailing slash on the target URL. This should work as intended, and there is little that can override this. It is quite likely you are seeing a cached response due to an earlier erroneous "permanent" redirect. 301 redirects are cached persistently by the browser by default.

Test first with 302 (temporary) redirects to avoid potential caching issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.