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

时间:2016-09-01 06:13:41

标签: php apache .htaccess codeigniter

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

5 个答案:

答案 0 :(得分:3)

使用以下内容更新您的代码并阅读评论

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>

答案 1 :(得分:1)

按照此顺序制定规则并修复http->https + www规则。最后减少了多余的规则:

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]

答案 2 :(得分:0)

我现在还没有深入到codeigniter,但请检查以下规则,

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ index.php/$1 [L]

请告诉我是否错过了什么。

答案 3 :(得分:0)

代码htaccess:

<IfModule mod_rewrite.c>

# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on

# NOTICE: If you get a 404 play with combinations of the following commented out lines
#AllowOverride All
#RewriteBase /wherever/codeginiter/is

# Restrict your site to only one domain
# !important USE ONLY ONE OPTION

# Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

# Remove index.php from URL
RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
RewriteCond %{THE_REQUEST}              ^[^/]*/index\.php [NC]
RewriteRule ^index\.php(.*)$            $1 [R=301,NS,L]

# Keep people out of codeigniter directory and Git/Mercurial data
# RedirectMatch 403 ^/(system|\.git|\.hg).*$

# Send request via index.php (again, not if its a real file or folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

<IfModule mod_php5.c>
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_php5.c>
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

在Application / config / config.php上删除index.php

$config['index_page'] = 'index.php';

答案 4 :(得分:0)

请在您.htaccess中使用此代码

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.([a-z]*)$
RewriteRule ^(.*)$ http://www.domain.%1/$1 [L,R=301]
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]