HTACCESS重定向单页到不同域而没有参数

发布于 2025-01-20 20:07:13 字数 604 浏览 3 评论 0原文

我有一个包含以下 htaccess 文件的网站:

RewriteCond %{THE_REQUEST} \s/+(.+?)\index.html/?[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?uri=$1 [NC,L,QSA]

我需要将两个页面重定向到不同的域。目前,我将它们设置为 302 重定向,并使用以下行:

Redirect 302 /some-url-slug https://example.com/some-new-url-slug

这确实可以重定向到新域,但问题是它将 uri 参数添加到重定向的 url,所以我得到类似的内容:

https://example.com/some-new-url-slug?uri=some-url-slug

我认为将其添加到重写规则之上会解决它,但它没有。我怎样才能省略该规则,但只针对 2 个特定页面?

I have a site with the following htaccess file:

RewriteCond %{THE_REQUEST} \s/+(.+?)\index.html/?[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?uri=$1 [NC,L,QSA]

There are two pages that I need to redirect to a different domain. Currently I have them setup as 302 redirects with the following line:

Redirect 302 /some-url-slug https://example.com/some-new-url-slug

This does work to redirect to the new domain but the issue is it's adding the uri parameter to the redirected url so I get something like:

https://example.com/some-new-url-slug?uri=some-url-slug

I thought adding it above the rewrite rule would solve it, but it does not. How can I omit that rule but only for 2 specific pages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再浓的妆也掩不了殇 2025-01-27 20:07:13

我认为在重写规则上方添加它可以解决它

重定向指令属于mod_alias,其他指令是mod_rewrite。放置redirect指令的位置,它将始终在 mod_rewrite之后进行处理重定向指令将保留查询字符串。

对于这两个特定的重定向,您需要使用mod_rewrite 重新写入,而是在文件的顶部。例如:

RewriteRule ^some-url-slug$ https://example.com/some-new-url-slug [QSD,R=302,L]

请注意,由Rewriterule指令匹配的URL路径不是从斜线开始的。 QSD(查询字符串丢弃)标志可确保丢弃初始请求中可能存在的任何查询字符串(否则默认情况下会通过)。

更新:

我刚刚意识到在URL上添加尾随 /加载原始页面。我需要为具有落后斜线的链接创建一个单独的重定向规则吗?

您可以在同一规则中包含此内容,并在模式结束时添加/?,使后斜线可选。例如:

RewriteRule ^some-url-slug/?$ https://example.com/some-new-url-slug [QSD,R=302,L]

现在,这同时匹配some-url-slug some-url-slug/和将其重定向到以前。

I thought adding it above the rewrite rule would solve it

The Redirect directive belongs to mod_alias, the other directives are mod_rewrite. It doesn't matter where you place the Redirect directive, it will always be processed after mod_rewrite. And the Redirect directive will preserve the query string.

You need to use a mod_rewrite RewriteRule instead, at the top of the file, for these two specific redirects. For example:

RewriteRule ^some-url-slug$ https://example.com/some-new-url-slug [QSD,R=302,L]

Note that the URL-path matched by the RewriteRule directive does not start with a slash. The QSD (Query String Discard) flag ensures that any query string that might be present on the initial request is discarded (otherwise it is passed through by default).

UPDATE:

I just realized that adding a trailing / to the url loads the original page. Do I need to create a seperate redirect rule for links that have the trailing slash?

You can include this in the same rule and make the trailing slash optional with the addition of /? at the end of the pattern. For example:

RewriteRule ^some-url-slug/?$ https://example.com/some-new-url-slug [QSD,R=302,L]

This now matches both some-url-slug or some-url-slug/ and redirects to the URL as before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文