mod_rewrite,匹配特定模式

发布于 2025-01-06 07:01:14 字数 1284 浏览 2 评论 0原文

标题很模糊,所以我很抱歉。

问题出在下面的代码中。我需要在关键字语句之后添加操作,但是当操作不存在时,我不希望该操作覆盖关键字。

带有操作的输入网址为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2025-01-13 07:01:14

我今天明白了这一点。基本上,在构建反向引用时,发生的情况是 htaccess 文件多次遍历规则。

我确实在原始关键字行中添加了一个跳过,但它会被忽略:

#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT,S=1]

#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]

#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]

当 htaccess 第二次运行时,S 将不再适用,然后它将命中 withoutactions 块。为了避免这个问题,我只是检查关键字是否已存在于查询字符串中,如果存在则跳过该规则:

#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT,S=1]

#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteCond %{QUERY_STRING} !keyword
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]

#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]

我希望这可以帮助遇到类似反向引用问题的人!

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:

#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT,S=1]

#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]

#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]

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:

#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT,S=1]

#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteCond %{QUERY_STRING} !keyword
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]

#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]

I hope this helps someone experiencing a similar problem with back references!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文