Redirect without www to an image

20 Views Asked by At

Here are that i have in .htaccess:

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

But this not work for me.

I want to put to an URL www.sitename.com/images/image-name.jpg and have in output the redirection to sitename.com/images/image-name.jpg

The main idea - no any 'www' in an url of images on site.

How can i proceed?

Thanks to all.

I have tried several directives in .htaccess, but no succeed.

1

There are 1 best solutions below

0
Ali Sheikhpour On

Second condition seems wrong as it is excluding images folder from the rule. You need to include the images folder instead:

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

also if you want to force all other addresses to have www (except images) you may also add this rule:

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