我想将所有路径重定向到域 https://www.test.com 除了以 /tutorial 开头的路径或图像的 url 之外。例如:
https://www.test.com/testing => https://www.test.com https://www.test.com/tutorial/xx => https://www.test.com/tutorial/xx
我有这个htaccess:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(test\.(?:com))$ [NC]
RewriteCond %{REQUEST_URI} !^/$ [NC]
RewriteCond %{REQUEST_URI} !^/tutorial/(.*)$ [NC]
RewriteCond %{REQUEST_URI} !\.(pdf|js|ico|gif|jpg|png|svg|css|rar|zip|tar\.gz|map)$ [NC]
RewriteRule .? https://www.test.com [R=301,L]
但是这段代码不起作用。即使我把 https://www.test.com/tutorial/xx 也总是重定向到 https://www.test.com。我还在 htaccess 测试器中进行了测试(https://technicalseo.com/tools/htaccess/),看起来 RewriteCond 应该没问题。但显然不是。
要在 Apache Web 服务器环境中使用 .htaccess 文件实现此重定向,您可以使用以下规则创建或修改 .htaccess 文件:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/tutorial/ [NC]
RewriteCond %{REQUEST_URI} !\.(png|jpg|jpeg|gif|bmp)$ [NC]
RewriteRule ^(.*)$ https://www.test.com/$1 [R=301,L]