Redirecting to https on .htaccess with single page application

Viewed 38

htaccess for a Single page application, and I want to add https redirect.

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php [QSA,L]
</ifModule>

I tried to apply it but web server redirect indefinitely

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php [QSA,L]

    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
</ifModule>
1 Answers

Have your htaccess rules file in following manner. Please make sure to clear your browser cache before testing your URLs.

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)/?$ https://example.com/$1 [L,NE,R=301]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/?$ index.php [QSA,L]

</ifModule>

Fixes in OP's attempts: First thing first changed regex of rewrite rule to match optional / in rules(both rules). Then applied needed flag NE to 1st rewrite rule. Also changed order of rules.

Related