.htaccess RewriteCond用于没有https

时间:2016-03-22 22:15:27

标签: .htaccess codeigniter mod-rewrite https rewrite

在指定的url(service / scripts /)路径上重写htaccess时出现问题。如果路径包含“服务/脚本并且只打开带有http的链接”,我不想重定向。所有其他链接应重定向到 https 。我正在使用Codeigniter框架。我尝试了以下代码TRY1和TRY2:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
#RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]
RewriteCond $1 !^(index\.php|(.*)\.swf|assets|service/*|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L] 
Header add Access-Control-Allow-Origin "https://url.info/" 

<IfModule mod_rewrite.c> 
# TURN SSL ON 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://www.url.info/$1 [R,L]

# TRY 1 
#RewriteCond %{HTTPS} on 
#RewriteCond %{REQUEST_URI} ^/service.*
#RewriteRule ^(.*)$ http://www.url.info/$1 [R=301,L] 
#RewriteCond %{HTTPS} off 
#RewriteCond %{REQUEST_URI} !^/service.*
#RewriteRule ^(.*)$ https://www.url.info/$1 [R=301,L] 

# TRY 2
#RewriteCond %{HTTPS} on 
#RewriteCond %{REQUEST_URI} ^/service/scripts/?.*$
#RewriteRule ^(.*)$ http://www.url.info/$1 [R=301,L] 
#RewriteCond %{HTTPS} off 
#RewriteCond %{REQUEST_URI} !^/service/scripts/?.*$ 
#RewriteRule ^(.*)$ https://www.url.info/$1 [R=301,L] 

# Removes index.php RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 [L] 
# If 404s, "No Input File" or every URL returns the same thing 
# make it /index.php?/$1 above (add the question mark) 

</IfModule>

它总是重定向像RewriteCond%{REQUEST_URI}!^ / service。*不是triggert或者是secound RewriteCond覆盖它......

请有人帮帮我吗? 最好的祝福, 戴夫

1 个答案:

答案 0 :(得分:1)

现在我在所有页面的HTML头中使用了PHP重定向来解决问题。

if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
if(!headers_sent()) {
    header("Status: 301 Moved Permanently");
    header(sprintf(
        'Location: https://%s%s',
        $_SERVER['HTTP_HOST'],
        $_SERVER['REQUEST_URI']
    ));
    exit();
}
}