php - 带有 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>
有人能帮我吗?提前感谢
解决方案
*更新
解决了!我用过:
<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>
推荐阅读
- fzf - 离线安装 fzf 模糊查找器
- java - 我如何循环/我可以循环一个 hashmap put() 吗?
- c# - NPOI 关于导入 excel - 返回 UI 内的 UI 而不是 UI 中的表
- c++ - 如何评估代码的复杂性(Big-O)?
- javascript - 显示计算步骤 p5.js
- excel - Word 冻结,当宏调用 Excel 打开时
- unit-testing - Mock Third party library (Razorpay) in Angular Unit tests?
- javascript - 有没有办法澄清这个JS中的真假陈述?
- email - 合并多个通过 DMARC 的 SPF TXT 记录的更好方法
- r - 如何计算R中表格中的百分比差异