简体   繁体   English

htaccess 将单个页面重定向到不带参数的不同域

[英]htaccess Redirect Single Page to Different Domain without Parameters

I have a site with the following.htaccess file:我有一个包含以下 .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]

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:目前,我使用以下行将它们设置为 302 重定向:

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:这确实可以重定向到新域,但问题是它将 uri 参数添加到重定向的 url 所以我得到类似的东西:

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?我怎样才能省略该规则但仅限于 2 个特定页面?

I thought adding it above the rewrite rule would solve it我认为将它添加到重写规则之上会解决它

The Redirect directive belongs to mod_alias, the other directives are mod_rewrite. Redirect指令属于mod_alias,其他指令是mod_rewrite。 It doesn't matter where you place the Redirect directive, it will always be processed after mod_rewrite.不管你把Redirect指令放在哪里,它总是在 mod_rewrite之后被处理。 And the Redirect directive will preserve the query string. Redirect指令将保留查询字符串。

You need to use a mod_rewrite RewriteRule instead, at the top of the file, for these two specific redirects.对于这两个特定的重定向,您需要在文件顶部使用 mod_rewrite RewriteRule 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.请注意,与RewriteRule指令匹配的 URL 路径不以斜杠开头。 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). QSD (查询字符串丢弃)标志确保丢弃初始请求中可能存在的任何查询字符串(否则默认情况下通过)。

UPDATE:更新:

I just realized that adding a trailing / to the url loads the original page.我刚刚意识到在 url 中添加尾随 / 会加载原始页面。 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.这现在匹配some-url-slugsome-url-slug/并像以前一样重定向到 URL。

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

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