htaccess特定于uri的重写规则

时间:2018-02-22 09:57:24

标签: .htaccess codeigniter mod-rewrite

我的htaccess文件中有以下内容

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # Block hidden directories
    RewriteRule "(^|/)\." - [F]

    # Prevent /health_check.php from using https
    RewriteCond %{REQUEST_URI} !(health_check\.php)$

    # Prevent /sns from using https but this DOES need codeigniter rewriting (see below)
    RewriteCond %{REQUEST_URI} !^/(sns)/

    # Reroute http to https
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

    # Prevent rewriting of domain for codeigniter
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

</IfModule>

我想做的是

  • 重写codeigniter网址(包括/ sns)

  • 将http请求转发给https(/health_check.php和/ sns除外)

  • 阻止隐藏目录

这一切似乎都有效,除了/ sns仍然转发到https并更改为/index.php/sns

1 个答案:

答案 0 :(得分:0)

将您的第二条规则更改为:

# Prevent /health_check.php from using https
RewriteCond %{THE_REQUEST} !/health_check\.php [NC]

# Prevent /sns from using https but this DOES need codeigniter rewriting (see below)
RewriteCond %{THE_REQUEST} !/sns [NC]

# Reroute http to https
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
  • 使用THE_REQUEST代替REQUEST_URI作为最后规则会将REQUEST_URI更改为/index.php
  • 请务必在新浏览器中对此进行测试。