You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置.htaccess实现访问site.com/profile时校验cookie中token并跳转

.htaccess 访问控制规则配置
  • 前置要求:Apache 环境需开启 mod_rewrite 模块,且站点对应目录的 AllowOverride 配置项需设置为 All,规则才会生效。

以下是可直接使用的配置代码:

RewriteEngine On

# 匹配 /profile 路径的访问请求
RewriteCond %{REQUEST_URI} ^/profile/?$
# 校验Cookie中是否存在非空的token字段
RewriteCond %{HTTP_COOKIE} !token=[^;\s]+ [NC]
# 未通过校验则重定向至登录页,302为临时重定向,L代表匹配后终止后续规则
RewriteRule ^(.*)$ /login [R=302,L]

如果不需要严格校验token非空,仅判断token字段是否存在,可将Cookie校验行替换为:
RewriteCond %{HTTP_COOKIE} !token= [NC]

内容的提问来源于stack exchange,提问作者Sergey Zheleshchenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 20:36:02