Mod_rewrite http-> https重定向除了一个URL

时间:2016-02-09 07:06:56

标签: .htaccess codeigniter mod-rewrite

好的,所以我试图这样做:

摘要:除了/ [a-f0-9] {11} /之外,所有网址都应该是https://www.domain.com/(。*),应该强制为http。

我有一套旧规则(见下文)看起来不太干净。 我已尝试添加它们以考虑/ [a-f0-9] {11}并最终重定向而不是重新映射,因此我最终得到www.domain.com/index.php/thepattern0

如何清理这些规则并使其有效?

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -MultiViews -Indexes
    RewriteEngine On
    RewriteBase /
    #redirect to www
    RewriteCond %{HTTP_HOST} ^domain.com [NC]
    RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

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

    #hide index.php
    RedirectMatch 404 .*php\.ini
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    #RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

1 个答案:

答案 0 :(得分:1)

您可以使用:

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /

#redirect to www
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#redirect to https
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+[a-f0-9]{11}[/?\s] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]

#redirect to http
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+[a-f0-9]{11}[/?\s] [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]

#hide index.php
RewriteRule \.(?:php|ini)$ [L,R=404,NC]

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