[英]HTACCESS 301 : How to redirect all urls to HTTPS except spammy urls with a specific character?
我一个月前发布了一个问题,答案很好 .HTACCESS 403:如何使用特定字符阻止 URL?): HTACCESS 403:如何使用特定字符阻止 url?
问题是,我将我的网站 HTTP 迁移到 HTTP S ,我想重定向所有网址,但我会用 410 代码阻止的具有特定字符的垃圾网址除外。
举例我想要的:
http://www.example.com/caterory/article-name/?vn/2022-06-24fivhg585.html ==> 410 code, without 301 to HTTPS
http://www.example.com/caterory/article-name/webhook.php?tw3fpage3rjnso530724 ==> 410 code, without 301 to HTTPS
http://www.example.com/caterory/article-name/football.php?fsmkfpagefgdg456 ==> 410 code, without 301 to HTTPS
错了,今天,垃圾网址有一个 301 代码,然后是一个 410 代码
http://www.example.com/caterory/article-name/?vn/2022-06-24fivhg585.html ==> 301 to https://www.example.com/caterory/article-name/?vn/2022-06-24fivhg585.html and then ==> 410.
http://www.example.com/caterory/article-name/webhook.php?tw3fpage3rjnso530724 ==> 301 to
https://www.example.com/caterory/article-name/webhook.php?tw3fpage3rjnso530724 and then ==> 410.
http://www.example.com/caterory/article-name/football.php?fsmkfpagefgdg456 ==> 301 to
https://www.example.com/caterory/article-name/football.php?fsmkfpagefgdg456 and then ==> 410.
我正在使用这些规则:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^.*$ https://www.%1%{REQUEST_URI} [L,NE,R=301]
RewriteEngine On
RewriteCond %{QUERY_STRING} ^vn/ [NC]
RewriteRule ^ - [R=410]
RewriteEngine On
RewriteCond %{THE_REQUEST} /webhook.php [NC]
RewriteRule ^ - [R=410]
RewriteEngine On
RewriteCond %{THE_REQUEST} /football.php [NC]
RewriteRule ^ - [R=410]
您是否有管理 301 重定向的想法,但具有特定字符/字符串页面的 URL 除外。
只是颠倒规则的顺序,所以你的阻塞指令是第一位的(因为它们应该是)。
也无需重复RewriteEngine
指令。
而不是使用THE_REQUEST
服务器变量(在您使用它的上下文中可能匹配太多),您应该只使用RewriteRule
模式(或者甚至将规则合并为一个)。
例如:
RewriteEngine On
# Blocking the following requests
RewriteCond %{QUERY_STRING} ^vn/ [NC]
RewriteRule ^ - [R=410]
RewriteRule /webhook\.php$ - [NC,R=410]
RewriteRule /football\.php$ - [NC,R=410]
# Canonical redirect
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
另请注意,我将最后一条规则中的正则表达式^.*$
简化为^
。
3 个阻止规则可以合并为一个(但这样做并没有任何好处)。 例如:
# Blocking the following requests (combined rule)
RewriteCond %{QUERY_STRING} ^vn/ [OR,NC]
RewriteCond %{REQUEST_URI} /webhook\.php$ [OR,NC]
RewriteCond %{REQUEST_URI} /football\.php$ [NC]
RewriteRule ^ - [G]
# Canonical redirect
:
注意: G
( gone
)只是R=410
的简写。
作为一般规则,指令的顺序应该是:
阻塞指令
外部重定向
内部改写
错了,今天,垃圾网址有一个 301 代码,然后是一个 410 代码
尽管这并不重要,但它可能会使用极少量的额外资源。 它最终仍然是410。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.