HTACCESS重定向单页到不同域而没有参数
我有一个包含以下 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重定向
指令属于mod_alias,其他指令是mod_rewrite。放置redirect
指令的位置,它将始终在 mod_rewrite之后进行处理。重定向
指令将保留查询字符串。对于这两个特定的重定向,您需要使用mod_rewrite
重新写入
,而是在文件的顶部。例如:请注意,由
Rewriterule
指令匹配的URL路径不是从斜线开始的。QSD
(查询字符串丢弃)标志可确保丢弃初始请求中可能存在的任何查询字符串(否则默认情况下会通过)。更新:
您可以在同一规则中包含此内容,并在模式结束时添加
/?
,使后斜线可选。例如:现在,这同时匹配
some-url-slug
或some-url-slug/
和将其重定向到以前。The
Redirect
directive belongs to mod_alias, the other directives are mod_rewrite. It doesn't matter where you place theRedirect
directive, it will always be processed after mod_rewrite. And theRedirect
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:Note that the URL-path matched by the
RewriteRule
directive does not start with a slash. TheQSD
(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:
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:This now matches both
some-url-slug
orsome-url-slug/
and redirects to the URL as before.