301将www版本重定向到非仅用于子目录

时间:2019-03-25 17:26:12

标签: .htaccess url redirect

尝试301将所有网址重定向到特定子目录中的non-www 。例如:

https://www.example.com/test

https://example.com/test

但是不重定向任何其他子目录,例如: https://www.example.com/test1应该保留www

更多示例:

https://www.example.com/test/country/city

https://example.com/test/country/city

1 个答案:

答案 0 :(得分:0)

您可以使用它将www的URL重定向到特定目录的non-www

RewriteEngine on

#the directory the rule should apply to
RewriteCond %{REQUEST_URI} ^/DirName/  [NC]
#check if the host  string starts with "www"
RewriteCond %{HTTP_HOST} ^www\. [NC]
#redirect all www urls to non-www
RewriteRule (.*) http://example.com%{REQUEST_URI}  [L,R=301]