Apache重写特定情况的规则
我有一个.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用REGEX交替将所有这些规则组合到一个规则中:
此模式
^(blog/| [2-6]/?$)
将匹配开始位置,然后blog/或
[2-6]
,然后是可选的/
和结束位置。对于其他查询字符串规则,您可以使用:
您无需在重定向规则中使用
https://%{http_host}
以使其与请求中的域相同,因此请这样做:You can combine all those rules into one rule using regex alternation:
This pattern
^(blog/|[2-6]/?$)
will match start position and then eitherblog/
or[2-6]
followed by optional/
and end position.For the other query string rule you can use:
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: