Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force http:// in htaccess?

I currently have this in my htaccess file to force https on all pages on my sign in and sign up pages:

# Force https://
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTPS} off
    RewriteCond $1 ^(sign-in|sign-up) [NC]
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

I want to force http whenever the page is not sign in or sign up. How do I do that?

EDIT 1:

I actually need to do the following:

  1. Force https:// for pages in sign-up and sign-in
  2. Force http:// for pages not in sign-up and sign-in
  3. Remove www
  4. Remove index.php

The relevant part of my .htaccess has this:

#
# Force https:// or http://
#
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTPS} off
    RewriteCond $1 ^(sign-in|sign-up) [NC]
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    #RewriteCond %{HTTPS} on
    #RewriteCond $1 !^(sign-in|sign-up) [NC]
    #RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    #RewriteCond %{HTTPS} on
    #RewriteCond %{REQUEST_URI} !^/(sign-in|sign-up)(/|$) [NC]
    #RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

#
# Supress "www" at the beginning of URLs. Source: HTML5 Boilerplate
#
<IfModule mod_rewrite.c>
    RewriteEngine On

    #RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>

#
# Supress "index.php"
#
<IfModule mod_rewrite.c>
    RewriteEngine On

    # Removes index.php from ExpressionEngine URLs
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
like image 978
StackOverflowNewbie Avatar asked Dec 13 '13 02:12

StackOverflowNewbie


1 Answers

Have your rules like this:

RewriteEngine On

# force HTTPS for sign-in|sign-up
RewriteCond %{HTTPS} off
RewriteRule ^(sign-in|sign-up)(/|$) https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R=301]

# force HTTP for NOT sign-in|sign-up
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(sign-in|sign-up)(/|$) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
like image 57
anubhava Avatar answered Sep 30 '22 01:09

anubhava