Reputation: 35
I installed an ssl certificate on my site, when i enter a url with "https", it loads the way it should, but when i enter a url with only "http" like "http://example.org/menu" it redirects me to "https://example.org/index.html" and i dont know why. This is what my htaccess contains
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^imgs$ imgs [NC,L,QSA]
RewriteRule ^static$ static [NC,L,QSA]
RewriteRule ^test$ index.html [NC,L,QSA]
RewriteRule ^login$ index.html [NC,L,QSA]
RewriteRule ^menu$ index.html [NC,L,QSA]
RewriteCond %{HTTPS} on!
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
All rules say index.html because it is an SPA, before i install de ssl and add the two latest lines it worked with no problem, but now every url that i enter with http redirects to "https://example.org/index.html"
Upvotes: 1
Views: 44
Reputation: 143946
The problem is the redirect is happening after you've rewritten the URL. Your redirects need to be before your rewrites. Also, the first two conditions are only being applied to the first rule, they're not globally applied. You probably want something along the lines of this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Redirect first, then apply any rewrites after the browser loads the new URL
RewriteCond %{HTTPS} on!
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# If any request is for an existing resource, stop the rewriting immediately
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^imgs$ imgs [NC,L,QSA]
RewriteRule ^static$ static [NC,L,QSA]
RewriteRule ^test$ index.html [NC,L,QSA]
RewriteRule ^login$ index.html [NC,L,QSA]
RewriteRule ^menu$ index.html [NC,L,QSA]
Upvotes: 1