首页 > 解决方案 > 带有 cookie 和多个条件的 htacces (if, else)

问题描述

我正在尝试使用 cookie 保护带有 .htaccess 的目录。重写规则将用户发送到基本目录中的登录 (php)。有用。当用户登录并获取 cookie 时,脚本会将他重定向到 .htaccess 所在的目录,我必须在其中制定其他重写规则。问题是 .htaccess 无法识别 cookie 并循环返回登录,而是识别它并重定向到目录。我的 apache 是 > 2.4 和 php7.0

在我的 .htaccess 中,我有:

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{HTTP_COOKIE} !(utente_xxx) [NC,OR]
    RewriteRule ^.*$ http://www.domain.xxx/login.php [R,L]

    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f

    # otherwise forward it to index.php
    RewriteRule . index.php
</IfModule>

我已经用谷歌搜索了,我发现使用 apache >2.4 我可以使用 If-Else,我尝试使用它,但我不是该语法的专家,并且我获得了内部服务器错误,错误为“.htaccess:不能解析条件子句:语法错误,日志中出现意外的 T_OP_NOT”。

这是我的 .htaccess 代码:

<If "%{HTTP_COOKIE} !(utente_xxx)">
   RewriteRule ^.*$ http://www.domain.xxx/login.php [R,L]
</If>
<Else>
<If "%{REQUEST_FILENAME} !-f">
   RewriteRule . index.php
   </If>
</Else>

有人能帮我吗?提前感谢

标签: phpapache.htaccessmod-rewrite

解决方案


*更新

解决了!我用过:

<IfModule mod_rewrite.c>
    RewriteEngine on

    # if a directory or a file exists, use it directly
    RewriteCond %{HTTP_COOKIE} (utente_xxx) [NC]
    RewriteCond %{REQUEST_FILENAME} !-f

    # otherwise forward it to index.php
    RewriteRule . index.php

        RewriteCond %{HTTP_COOKIE} !(utente_xxx) [NC]
    RewriteRule ^.*$ http://www.domain.xxx/login.php [R,L]

</IfModule>

推荐阅读