使用 www 强制 HTTPS 并在 CodeIgniter 中删除 index.php .htaccess

Posted

技术标签:

【中文标题】使用 www 强制 HTTPS 并在 CodeIgniter 中删除 index.php .htaccess【英文标题】:Forcing HTTPS with www and remove index.php .htaccess in CodeIgniter 【发布时间】:2017-01-08 21:17:32 【问题描述】:

如何使用 htaccess 实现所有这些。到目前为止,我已经--

RewriteEngine On

删除 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

强制 SSL 和非 www 到 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]

知道它为什么会发生,但无法找出一个规则来组合我需要的一切并使其发挥作用。

需要强制 https 并删除 index.php 和非 www 到 www。 答案将不胜感激并提前感谢

【问题讨论】:

您的托管服务提供商的名称是什么???? www.ifsc-bank.com 我的托管服务提供商是 unionhost.com 请打开 config.php 文件并找到 $config['uri_protocol'] 并设置 $config['uri_protocol']="REQUEST_URI" 。 【参考方案1】:

使用下面更新您的代码并阅读 cmets

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>

【讨论】:

网站未打开...获取空白页 那是不同的问题,你可以输入 www.name.com 并看到它重定向到 name.com 其中名称 = 你的服务器名称,如果它工作,请检查你的错误日志文件或启用数据库错误日志记录在 config.php 即 $config['log_threshold']='1';和 database.php 即 $db['default']['db_debug'] = TRUE;【参考方案2】:

将您的规则按此顺序排列,并修复http-&gt;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]

【讨论】:

【参考方案3】:

我现在还没有深入研究 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]

如有遗漏请告知。

【讨论】:

【参考方案4】:

请在你的.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]

【讨论】:

【参考方案5】:

代码 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';

【讨论】:

我需要强制执行 https

以上是关于使用 www 强制 HTTPS 并在 CodeIgniter 中删除 index.php .htaccess的主要内容,如果未能解决你的问题,请参考以下文章

使用 .htaccess 为 CloudFlare 用户强制使用 HTTPS 和非 WWW

nginx配置CI重写规则,codeigniter

通过 htaccess 强制非 www 和 https

Apache强制WWW跳转以及强制HTTPS加密跳转的方法

.htaccess:在所有页面上强制 www,在一个页面上强制 https,在所有其他页面上强制 http

apache_conf 在htaccess中强制使用HTTPS和WWW