Apache重写特定情况的规则

发布于 2025-02-03 16:26:09 字数 931 浏览 5 评论 0原文

我有一个.htaccess文件和几个重定向规则。

例如:

RewriteRule ^blog/migrations/?$ / [R=301,L,NE]
RewriteRule ^blog/digital/?$ / [R=301,L,NE]

基本上,我所有的博客URL都已删除,但我发现每个博客URL的重定向规则重定向到主页。但是我希望这是一个规则,这样的事情是这样的:

RewriteRule ^blog/{anyUrl}/?$ / [R=301,L,NE]

我也有奇怪的重定向,例如:

RewriteRule ^2/?$ / [R=301,L,NE]
RewriteRule ^3/?$ / [R=301,L,NE]
RewriteRule ^4/?$ / [R=301,L,NE]
RewriteRule ^5/?$ / [R=301,L,NE]
RewriteRule ^6/?$ / [R=301,L,NE]

我希望它们成为一个规则,例如:

RewriteRule ^{[2-6]}/?$ / [R=301,L,NE]

任何人都可以帮助这些规则吗?

//编辑

也有一个规则,该规则将URL与查询字符串重定向:

RewriteCond %{QUERY_STRING} .
RewriteRule ^(.*)/?$ /$1 [QSD,R=301,L]

但是该规则删除了所有查询字符串。如何从以utm _开头的规则特定查询字符串中排除?

我该如何获取域名?我在两个不同的域上有我的网站。例如:

RewriteRule ^posts/? https://{domain}/somewhere

I have a .htaccess file and a couple of redirect rule.

For example:

RewriteRule ^blog/migrations/?$ / [R=301,L,NE]
RewriteRule ^blog/digital/?$ / [R=301,L,NE]

Basically, all my blog urls have been removed but i found redirects rule for every single blog url to redirect to main page. But i want this to be in one rule, something like this:

RewriteRule ^blog/{anyUrl}/?$ / [R=301,L,NE]

Also i have strange redirects, such as:

RewriteRule ^2/?$ / [R=301,L,NE]
RewriteRule ^3/?$ / [R=301,L,NE]
RewriteRule ^4/?$ / [R=301,L,NE]
RewriteRule ^5/?$ / [R=301,L,NE]
RewriteRule ^6/?$ / [R=301,L,NE]

And i want them to be one rule, for example:

RewriteRule ^{[2-6]}/?$ / [R=301,L,NE]

Can anyone help with those rule, i am not really good at regexps.

//Edited

Also i have a rule that redirects urls with query strings:

RewriteCond %{QUERY_STRING} .
RewriteRule ^(.*)/?$ /$1 [QSD,R=301,L]

But this rule removes all query strings. How can i exclude from the rule specific query strings that starts with utm_?

And how can i get the domain name? I have my website on two different domains. For example:

RewriteRule ^posts/? https://{domain}/somewhere

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

谁的新欢旧爱 2025-02-10 16:26:09

您可以使用REGEX交替将所有这些规则组合到一个规则中:

RewriteRule ^(blog/|[2-6]/?$) https://%{HTTP_HOST}/ [R=301,L,NC]

此模式^(blog/| [2-6]/?$)将匹配开始位置,然后blog/或[2-6],然后是可选的/和结束位置。


对于其他查询字符串规则,您可以使用:

RewriteCond %{QUERY_STRING} .
RewriteCond %{QUERY_STRING} !(^|&)utm_ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [QSD,R=301,L,NE]

RewriteRule ^posts/? https://{domain}/somewhere

您无需在重定向规则中使用https://%{http_host}以使其与请求中的域相同,因此请这样做:

RewriteRule ^posts/? https://%{HTTP_HOST}/somewhere [L,R]

You can combine all those rules into one rule using regex alternation:

RewriteRule ^(blog/|[2-6]/?$) https://%{HTTP_HOST}/ [R=301,L,NC]

This pattern ^(blog/|[2-6]/?$) will match start position and then either blog/ or [2-6] followed by optional / and end position.


For the other query string rule you can use:

RewriteCond %{QUERY_STRING} .
RewriteCond %{QUERY_STRING} !(^|&)utm_ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [QSD,R=301,L,NE]

RewriteRule ^posts/? https://{domain}/somewhere

You don't need to use https://%{HTTP_HOST} here in redirect rules to make it same domain as in the request, so make it:

RewriteRule ^posts/? https://%{HTTP_HOST}/somewhere [L,R]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文