mod_rewrite,匹配特定模式
标题很模糊,所以我很抱歉。
问题出在下面的代码中。我需要在关键字语句之后添加操作,但是当操作不存在时,我不希望该操作覆盖关键字。
带有操作的输入网址为 http://websitenamehere/uk/en/meet-us/update 并且没有: http://websitenamehere/uk/en/meet-us
所以我想要这些操作仅当后续内容是 4 个或更多字符的字符串时才会触发子句。此 htaccess 规则不应取决于进行比较时设置的国家/地区或语言。
如果我错过了任何内容,请告诉我,我会更新问题。我知道它应该相当简单,但目前我只是在 htaccess 和 learnring 中犯错误。
#language
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})/([a-z]{2})(?:/(.*)|)$ /$1/$3?lang=$2 [NC,QSA,PT]
#country
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})(?:/(.*)|)$ /$2?country=$1 [NC,QSA,PT]
#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT]
#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]
#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ /$1.php [NC,QSA,PT]
The title is quite ambiguous so I apologise.
The problem lies in the following code. I need to add actions after the keyword statement however I do not want the action to override the keyword when action is not present.
The input url with actions is http://websitenamehere/uk/en/meet-us/update
and without: http://websitenamehere/uk/en/meet-us
So I want the actions clause to be triggered only when what proceeds it is a string of 4 or more characters. This htaccess rule should not depend on the country or lang being set when making the comparison.
If I have missed anything please let me know and I will update the question. I know it should be fairly simple but at the moment I am just blundering through htaccess and leanring as I go.
#language
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})/([a-z]{2})(?:/(.*)|)$ /$1/$3?lang=$2 [NC,QSA,PT]
#country
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})(?:/(.*)|)$ /$2?country=$1 [NC,QSA,PT]
#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT]
#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]
#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ /$1.php [NC,QSA,PT]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我今天明白了这一点。基本上,在构建反向引用时,发生的情况是 htaccess 文件多次遍历规则。
我确实在原始关键字行中添加了一个跳过,但它会被忽略:
当 htaccess 第二次运行时,S 将不再适用,然后它将命中 withoutactions 块。为了避免这个问题,我只是检查关键字是否已存在于查询字符串中,如果存在则跳过该规则:
我希望这可以帮助遇到类似反向引用问题的人!
I figured this out today. Basically what is happening is the htaccess file when building a back reference goes through the rules more than once.
I did add a skip into the original keyword line but it would simply be ignored:
When the htaccess ran the second time the S would no longer apply and it would then hit the withoutactions block. To circumvent this I simply checked if keyword already existed in the query string and if so to skip the rule:
I hope this helps someone experiencing a similar problem with back references!