不带 www 重定向到图像

问题描述 投票:0回答:1

这是我在 .htaccess 中的:

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteCond %{REQUEST_URI} !^/images/.*$ 
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

但这对我不起作用。

我想输入一个网址 www.sitename.com/images/image-name.jpg 并在输出中重定向到 sitename.com/images/image-name.jpg

主要思想 - 网站图像的 URL 中没有任何“www”。

我该如何继续?

谢谢大家。

我在.htaccess中尝试了几个指令,但没有成功。

web redirect imageurl
1个回答
0
投票

第二个条件似乎是错误的,因为它从规则中排除了 images 文件夹。您需要包含 images 文件夹:

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

如果您想强制所有其他地址都有 www(图片除外),您也可以添加此规则:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/images/.*$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
© www.soinside.com 2019 - 2024. All rights reserved.