Search code examples
apache.htaccesssslurl-rewritingapache2

Double Slash behind URL


I'm forwarding any non www and non SSL connection to https://www.* with this code in my .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.tld/$1 [R=301,L]

But when I try to put this exact code in my 000-default.conf and 000-default-le-ssl.conf, it puts a double slash (https://www.domain.tld**//**) behind the URL. Why?


Solution

  • But when I try to put this exact code in my 000-default.conf and 000-default-le-ssl.conf, it puts a double slash (https://www.domain.tld//) behind the URL. Why?

    It is because in http server configs leading slash is also matches in RewriteRule unlike .htaccess files.

    You can use this rule to make it behave same in both type of files:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
    RewriteCond %{HTTPS} off
    RewriteRule ^/?(.*)$ https://www.domain.tld/$1 [R=301,NE,L]
    

    Or else:

    RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://www.domain.tld%{REQUEST_URI} [R=301,NE,L]