.htaccess - 如何根据先行正则表达式规则比较 url 中的名称?

.htaccess - how to compare names in url based on lookahead regex rule?

给定以下 url 结构:http://localhost/name/name

以及以下环境变量:

RewriteRule ^.* - [E=ORIG_REQUEST:name/name]
RewriteRule ^.* - [E=DIR_NAME:name]
RewriteRule ^.* - [E=FILE_NAME:name]

如何根据 lookahead 正则表达式检查 DIR_NAME 是否等于 FILE_NAME

我有:

RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{ENV:ORIG_REQUEST} ^([^/]+)/(?=)$ [NC]
RewriteRule - [E=IS_EQUAL:true]

灵感来自 .htaccess - how to remove repeated words in url?

试试这个正则表达式。如果 dir 不等于 file

,它将不匹配
   :\/\/\w+\/(\w+)\/(?=)

演示 here.

How to check if DIR_NAME is equal to FILE_NAME based on a lookahead regex rule?

这应该适合你:

RewriteRule ^ - [E=ORIG_REQUEST:name/name]
RewriteRule ^ - [E=DIR_NAME:name]
RewriteRule ^ - [E=FILE_NAME:name]

RewriteCond %{ENV:DIR_NAME}#%{ENV:FILE_NAME} ^([^#]+)#$
RewriteRule ^ - [E=IS_EQUAL:true]
  • # 仅用作 2 个变量之间的分隔符。它可以是任何东西。
  • ([^#]+) 捕获匹配组 #1
  • 中的第一个变量 DIR_NAME
  • 是从第一个字符串
  • 捕获的值的反向引用