peke_peke
peke_peke

Reputation: 551

apache mod rewrite (all HTTP requests to HTTPs)

After setting up letsencrypt on a vps my these are the rewrite conditions set by letsencrypt:

RewriteEngine on
RewriteCond %{SERVER_NAME} =xy.com [OR]
RewriteCond %{SERVER_NAME} =www.xy.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

It works fine, but I would like to redirect requests to 'https://www.xy.com to https too. I tried using this code:

RewriteEngine on
RewriteCond %{SERVER_NAME} =xy.com [OR]
RewriteCond %{SERVER_NAME} =www.xy.com [OR]
RewriteCond %{SERVER_NAME} =https://www.xy.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

It doesn't work. Any idea what to do?

None of the answers worked. Here's the file which is placed in my www/html/xy/public folder. All requests will point to this, I don't know if this maybe causes the problem?

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

Upvotes: 1

Views: 3930

Answers (2)

Joe
Joe

Reputation: 4917

To make all HTTP requests go to HTTPS all you will need to use is:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

It basically says, if HTTPS is not ON, then it will change it to https://

Upvotes: 3

Amit Verma
Amit Verma

Reputation: 41219

Try :

RewriteEngine on


RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule ^ https://%1%{REQUEST_URI} [NC,L]

This will redirect both http or www to https://

Or you can use this :

RewriteEngine on
RewriteCond %{SERVER_NAME} ^(?:www\.)?(xy)\.com$
RewriteRule ^ https://%1%{REQUEST_URI} [END,QSA,R=permanent]

Upvotes: 1

Related Questions