duanguanzai6181 2018-06-21 15:56
浏览 123

htaccess与cookie和多个条件(如果,否则)

I'm trying to protect with cookie a directory with an .htaccess. The rewrite rule sends the user to the login (php) in the base directory. It works. When the user login and take the cookie, the script redirects him to the directory where there is the .htaccess where I have to make other rewrite rules. The problem is that the .htaccess doesn't recognize the cookie and makes a loop going back to the login that, instead, recognize it and redirects to the directory. My apache is > 2.4 with php7.0

In my .htaccess I have:

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

I've googled and I found that with apache >2.4 I can use If-Else, I've tried to use it but I'm not expert of that syntax and I obtain an Internal Server Error with the error ".htaccess: Cannot parse condition clause: syntax error, unexpected T_OP_NOT" in the logs.

This is my .htaccess code:

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

Can someone help me? Thanks in andvance

  • 写回答

1条回答 默认 最新

  • douna1895 2018-06-21 16:16
    关注

    *UPDATE

    Solved! I've used:

    <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序