2

I have a website where I currently force example.com visits to www.example.com, and it has an SSL certificate, so I also force HTTP to HTTPS. My SSL certificate covers example.com and www.example.com.

I want to set up a static domain to serve images for my website, and I don't want to go through the trouble of updating my SSL or paying for a wildcard SSL... so my idea was to somehow route requests for static content to the example.com, while everything else gets forced to www.example.com.

So, I have in my .htaccess, the following snippet that forces the HTTPS:

  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

and the following snippet that forces the www.

  RewriteCond %{HTTP_HOST} ^mydomain\..+$ [NC]
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

How, exactly, should I change this so that if the request is for an image, it goes to https://example.com/..., and if it's not, then it forces the www and goes to https://www.example.com/...?

3

Method 1

With RewriteCond you can add this (after your existing rules):

RewriteCond %{HTTP_HOST} ^www\.example [NC]
RewriteCond %{HTTP_HOST} \.(jpg|jpeg|png|gif|ico|icon)$ [NC]
RewriteRule .* https://example.com%{REQUEST_URI} [L,R=301]

If the request is done on www.example.com, and the file requested end by jpg or jpeg or png or gif or ico or icon (case not sensitive) it will redirect to https://example.com%{REQUEST_URI} with status 301 (redirect permanent), but you need to change this to not do a redirect loop:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\..+$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Please note that I changed the following code:

  • FROM: http://www.%{HTTP_HOST}%{REQUEST_URI}
  • TO: https://www.%{HTTP_HOST}%{REQUEST_URI}

This should prevent 1 useless redirect from HTTP:// to HTTPS://.

Method 2

If you want to redirect in each case example.com to www.example.com (except images) then you will have to do the following:

RewriteCond %{HTTPS} off
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^example [NC]
RewriteCond %{HTTP_HOST} !\.(jpg|jpeg|png|gif|ico|icon)$ [NC]
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.example [NC]
RewriteCond %{HTTP_HOST} \.(jpg|jpeg|png|gif|ico|icon)$ [NC]
RewriteRule .* https://example.com%{REQUEST_URI} [L,R=301]
  • You seem to be switching between SERVER_NAME and HTTP_HOST (your code and the OPs it seems). I think HTTP_HOST should be used throughout. Depending on the server config, SERVER_NAME might not be the expected value (ie. the value from the request in this case). Method 2 - I think it would be preferable to make the exceptions for the static images first, before the existing canonical redirects. Otherwise, every request for static content will get redirected (twice, or even a redirect loop?). Or, perhaps remove some L flags, but I think changing the order would be preferable. – MrWhite Nov 4 '15 at 0:10
  • answer fixed, one rewrite rule was useless and creating a loop, i had to go fast yesterday so i didn't re-read my answer – Froggiz Nov 4 '15 at 9:23
1

First of all, you should start by redirecting all website traffic by using a redirect statement which is optional but highly recommended by Apache:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com 
    # [ Http to Https ]
    Redirect 301 / https://www.example.com/
</VirtualHost>

Then you can redirect your images using the following code:

<VirtualHost *:443>
    ServerName www.example.com
    RedirectMatch 301 \.(jpg|jpeg|png|gif|ico|icon)$ https://example.com/%{REQUEST_URI}
</VirtualHost>
<VirtualHost *:443>
    ServerName example.com
</VirtualHost>
  • 1
    Is this for use in an .htaccess file? Also, I already have other RewriteRules in use in my .htaccess file, and according to the link you shared: "The use of RewriteRule to perform this task may be appropriate if there are other RewriteRule directives in the same scope. This is because, when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file." – Gary Reckard Nov 3 '15 at 17:16
  • It also says "In the case of the http-to-https redirection, the use of RewriteRule would be appropriate if you don't have access to the main server configuration file, and are obliged to perform this task in a .htaccess file instead." – Gary Reckard Nov 3 '15 at 17:16
  • <VirtualHost> can't be used in .htaccess – Froggiz Nov 3 '15 at 17:19
  • That's what I thought. My specific case requires that I do this in .htaccess, but thanks for your quick reply and alternate suggestion. Please answer again if you know how to do this in .htaccess – Gary Reckard Nov 3 '15 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.