htaccess 301重定向整个网站,但有例外

7

我正在尝试创建一个htaccess文件来重定向我的整个网站,但需要有一些例外情况,但我无法使其工作。我需要重定向整个网站,并提供一个特定的重定向,同时排除两个页面。下面是我的非工作示例。谢谢!

RewriteCond %{REQUEST_URI} !^/events/index.html
RewriteCond %{REQUEST_URI} !^/calendar/index.html
Redirect 301 /info/faq.html http://mynewsite.com/my-page
Redirect 301 / http://mynewsite.com

请查看以下内容(带有301重定向域名的例外情况)https://helponnet.com/2021/05/01/redirect-domain-with-exception/ - Amit Verma
2个回答

20

您试图混合使用 mod_rewritemod_alias,但是 RewriteCond 语句无法对 Redirect 语句进行条件约束,因为它们不来自同一个模块。

如果我正确理解您试图实现的内容,我认为您需要类似于以下方式的代码:

RewriteEngine On

RewriteCond %{REQUEST_URI} !=/events/index.html
RewriteCond %{REQUEST_URI} !=/calendar/index.html
RewriteCond %{REQUEST_URI} !=/info/faq.html
RewriteRule ^.*$ http://mynewsite.com/$0 [R=301,L]

Redirect 301 /info/faq.html http://mynewsite.com/my-page

我正在尝试完成这个变体 - 我想重定向所有东西,除非请求是针对特定目录的(可以是各种文件类型) - 但它没有起作用:[代码] RewriteEngine On RewriteCond %{REQUEST_URI} !=/sites/default/files/imports RewriteRule ^.*$ http://mynewsite.com/$0 [R=301,L]Redirect 301 / http://mynewsite.com/ [/代码] - Scott Szretter
3
尝试使用%{REQUEST_URI} !^/sites/default/files/imports,因为!=仅适用于精确字符串比较。 - Tim Stone

4

我有一个类似的问题。试图重定向整个域名,但除了它的robots.txt文件。Tim的答案对我没用,但这个方法有效。

RewriteEngine On
RewriteRule robots.txt - [L]
RewriteRule ^.*$ http://www.newsite.com/$0 [R=301,L]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接