Search code examples
apache.htaccessmod-rewrite

Page wont redirect properly if folder name contains a dot


In order to renew my SSL certificate I'm required to create a folder like https://example.com/.well-known/pki-validation/ and put a file in there so that it's accessible. I did so but as I try to access the file I get an error that says the page is not redirecting properly. How can I fix this?

I tried the .htaccess below but it's not working:

Redirect 301 /~mysite/.well-known/pki-validation https://www.example.com/.well-known/pki-validation

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^([A-Za-z0-9]+).html$ https://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/?$ index.php?s=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ index\.php\?s=([^\s]*)
RewriteRule ^/?(.*?)/?$ %1?%2%3 [L,R=301]

Solution

  • You need to prevent any requests to /.well-known/pki-validation/ from being redirected (by the directives that occur later in the file).

    The files in this directory must be directly accessible at the HTTP URL stated, with no redirection.

    For example, at the top of the root .htaccess file, immediately after the RewriteEngine directive:

    # Prevent requests to "/.well-known/pki-validation/...." being processed
    RewriteRule ^\.well-known/pki-validation/ - [L]
    

    Redirect 301 /~mysite/.well-known/pki-validation https://www.example.com/.well-known/pki-validation
    

    Not sure what you are trying to do here, but this directive should be removed. ~mysite looks like an Apache per-user directory, but if you are using a "per-user directory" then requests to this directory/file will fail and the SSL cert will not be renewed.

    Redirect is also a mod_alias directive and consequently runs after the existing mod_rewrite directives, despite it appearing first in the config file.