简体   繁体   English

HTACCESS 301:如何将所有网址重定向到 HTTPS,但带有特定字符的垃圾网址除外?

[英]HTACCESS 301 : How to redirect all urls to HTTPS except spammy urls with a specific character?

I posted a question one month ago with great answers .HTACCESS 403: How to block URL with a specific character?): HTACCESS 403: How to block url with a specific character?我一个月前发布了一个问题,答案很好 .HTACCESS 403:如何使用特定字符阻止 URL?): HTACCESS 403:如何使用特定字符阻止 url?

The problem is, I migrated my website HTTP to HTTP S and I would like to redirect all urls, except spammy urls whith a specific caracter that I would block with 410 code.问题是,我将我的网站 HTTP 迁移到 HTTP S ,我想重定向所有网址,但我会用 410 代码阻止的具有特定字符的垃圾网址除外。

Exemple what I would like:举例我想要的:

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

Wrong, today, the spammy urls have a 301 code, and then a 410 code错了,今天,垃圾网址有一个 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.

I'm using these rules:我正在使用这些规则:

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]

Do you have an idea to manage the 301 redirection except URLs with a specific character / string pages.您是否有管理 301 重定向的想法,但具有特定字符/字符串页面的 URL 除外。

Just reverse the order of the rules, so your blocking directives are first (as they should be).只是颠倒规则的顺序,所以你的阻塞指令是第一位的(因为它们应该是)。

There is also no need to repeat the RewriteEngine directive.也无需重复RewriteEngine指令。

Instead of using THE_REQUEST server variable (which is perhaps matching too much in the context you are using it), you should just use the RewriteRule pattern (or even combine the rules into one).而不是使用THE_REQUEST服务器变量(在您使用它的上下文中可能匹配太多),您应该只使用RewriteRule模式(或者甚至将规则合并为一个)。

For example:例如:

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]

Note also that I simplified the regex ^.*$ in the last rule to just ^ .另请注意,我将最后一条规则中的正则表达式^.*$简化为^

The 3 blocking rules can be combined into one (but does not really serve any benefit to do so). 3 个阻止规则可以合并为一个(但这样做并没有任何好处)。 For example:例如:

# 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
:

NB: G ( gone ) is just shorthand for R=410 .注意: Ggone )只是R=410的简写。

As a general rule, the order of your directives should be:作为一般规则,指令的顺序应该是:

  1. Blocking directives阻塞指令

  2. External redirects外部重定向

  3. Internal rewrites内部改写

Wrong, today, the spammy urls have a 301 code, and then a 410 code错了,今天,垃圾网址有一个 301 代码,然后是一个 410 代码

Although this doesn't really matter, except that it potentially uses a minuscule amount of additional resources.尽管这并不重要,但它可能会使用极少量的额外资源。 It's still ultimately a 410.它最终仍然是410。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM