假设我有一个域名叫:www.example.com/today.php?id=123。我让它使用以下代码重定向到 url www.example.com/yes/123/some-name:
Rewrite Rule ^yes/123/some-name$ today.php?id=123
但是现在我想让该链接在从https调用时始终重定向到http。因此,我通过添加以下内容尝试了以下代码:
RewriteCond %{HTTPS} on
Rewrite Rule ^today.php(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]
发生的情况是它被重定向到 http 但使用以下 url:www.example.com/today.php?id=123 而不是保留原始 www.example.com/yes/123/some-name。我做错了什么?
谢谢!
更改规则顺序:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^yes/[^/]+/.*$ http://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R=301]
RewriteRule ^yes/[^/]+/.*$ today.php?id=123 [L,QSA,NC]
一般来说,在重写规则之前保留你的 301 规则。
您需要确保重定向在内部路由的规则之前。您需要与清理的 URL 匹配,而不是内部映射的 URL。所以像这样:
RewriteCond %{HTTPS} on
RewriteRule ^yes/[0-9]+/ http://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]
RewriteRule ^yes/123/some-name$ today.php?id=123 [L]