Htaccess 重定向 - 在一个重定向中组合许多规则

Htaccess Redirects - Combine many rules in one single redirect

我正在尝试在单个重定向中组合更多规则,现在我有很多规则和很多重定向,如附图所示,但出于 SEO 目的,这不是一个好的行为。

故事是这样的:我有一个旧的 url,它已经不存在了,还有一个新的,我想在其中重定向。如果请求的 url 没有 www.和 https 然后我想添加 them.Also 如果 url 末尾有斜杠,我想将其删除。 现在一切正常,但有很多步骤。

这是我的 .htaccess 文件中的内容:

301 从旧的 url 重定向到新的 www.

RewriteRule ^source1/source2$ https://www.domain.com/destination1/destination2 [L,R=301]

删除 URL

末尾的斜线
RewriteCond %{REQUEST_URI} !^/system [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ / [R=301,L,QSA]

重定向到 https

#-----------------redirect to https-----------------
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www.domain.com(.*)$ [NC]
RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [L,R=301]

我在这个指令中使用了所有这些规则:

<IfModule mod_rewrite.c>

这样可以避免多个 301 用于 SEO 目的:

# specific redirects
RewriteRule ^source1/source2/?$ https://www.domain.com/destination1/destination2 [L,R=301,NC]

# Remove the slash from the end of URL
RewriteCond %{REQUEST_URI} !^/system [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ https://www.domain.com/ [R=301,L,NE]

#-----------------redirect to https-----------------
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [L,R=301,NE]

确保在测试此更改之前完全清除浏览器缓存。