如何使用 .htaccess 将除少数文件之外的所有内容从无 www 重定向到 www?

如何使用 .htaccess 将除少数文件之外的所有内容从无 www 重定向到 www?

如何将所有内容从无 www 重定向到 www除了少数文件,使用 .htaccess?

我正在使用以下 .htaccess 将 no-www 重定向到 www。

.htaccess

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^[^.]+\.[^.]+$ [nc]
rewriterule ^(.*)$ http://www.%{http_host}/$1 [r=301,nc]

如何为少数文件添加例外?
例如:
1)example.com/page1.html 不应重定向到 www.example.com
2)example.com/xml/page2.xml 不应重定向到 www.example.com

除 example.com/page1.html 和 example.com/xml/page2.xml 之外的所有内容都应重定向到 www.example.com

答案1

你可以链接你的重写条件指令,例如:

RewriteEngine on
RewriteCond %{REQUEST_URI} ! ^/page1\.html$
RewriteCond %{REQUEST_URI} ! ^/xml/page2\.xml$
RewriteCond %{http_host} ^[^.]+\.[^.]+$ [nc]
RewriteRule ^(.*)$ http://www.%{http_host}/$1 [r=301,nc]

相关内容