Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing HTTPS with www and remove index.php .htaccess in CodeIgniter

Tags:

How can I achieve all of this using htaccess. Thus far I have--

RewriteEngine On

To remove index.php

RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

To enforce SSL and non www to www

RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^(www|abc|cde|efe) [NC]  #Subdomain abc and cde
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

know why its happening but can't figure out a rule to combine everything I need and make it work.

need force to https and remove index.php and non-www to www. Answers will appreciated and thanks in advance

like image 440
Harshad Hirapara Avatar asked Sep 01 '16 06:09

Harshad Hirapara


People also ask

What is htaccess in CodeIgniter?

htaccess file in CodeIgniter. htaccess is the shortened used for Hypertext Access, which is a powerful configuration file that controls the directory “. htaccess”. It is used by Apache based web servers to control various server features.

How do I remove a site index?

Option #1: Use the robots meta-tag with NOINDEX If the page already has the robots meta-element with the value INDEX, you can simply replace it with NOINDEX. Done. Now you just have to wait until the desired URL is removed from the Google index.


2 Answers

update your code with below and have a read the comments too

Options +FollowSymLinks
 <IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    #First rewrite any request to the wrong domain to use the correct one (here www.)
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    #Now, rewrite to HTTPS:
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]   

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

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

</IfModule>
like image 198
AmmyTech Avatar answered Oct 11 '22 13:10

AmmyTech


Have your rules in this order with a fix for http->https + www rule. Finally cut down redundant rules:

DirectoryIndex index.php
RewriteEngine On

# To enforce SSL and non www to www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(www|abc|cde|efe)\. [NC]
RewriteRule ^ https://www.example.com/$1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
like image 45
anubhava Avatar answered Oct 11 '22 12:10

anubhava