Mobile app version of vmapp.org
Login or Join
XinRu657

: Need .htaccess mod_rewrite for serving static content from subdomain, but forcing www otherwise I have a website where I currently force example.com visits to www.example.com, and it has an SSL

@XinRu657

Posted in: #Cdn #Htaccess #ModRewrite #Subdomain

I have a website where I currently force example.com visits to example.com, and it has an SSL certificate, so I also force HTTP to HTTPS. My SSL certificate covers example.com and 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 example.com.
So, I have in my .htaccess, the following snippet that forces the HTTPS:

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


and the following snippet that forces the
RewriteCond %{HTTP_HOST} ^mydomain..+$ [NC]
RewriteRule ^ 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 example.com/..., and if it's not, then it forces the www and goes to www.example.com/...?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @XinRu657

2 Comments

Sorted by latest first Latest Oldest Best

 

@Miguel251

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 .* example.com%{REQUEST_URI} [L,R=301]


If the request is done on 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 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 ^ www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Please note that I changed the following code:


FROM: www.%{HTTP_HOST}%{REQUEST_URI} TO: www.%{HTTP_HOST}%{REQUEST_URI}


This should prevent 1 useless redirect from to .
Method 2

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

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

RewriteCond %{HTTP_HOST} ^example [NC]
RewriteCond %{HTTP_HOST} !.(jpg|jpeg|png|gif|ico|icon)$ [NC]
RewriteRule .* 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 .* example.com%{REQUEST_URI} [L,R=301]

10% popularity Vote Up Vote Down


 

@Cugini213

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 example.com
# [ Http to Https ]
Redirect 301 / www.example.com/ </VirtualHost>


Then you can redirect your images using the following code:

<VirtualHost *:443>
ServerName example.com RedirectMatch 301 .(jpg|jpeg|png|gif|ico|icon)$ example.com/%{REQUEST_URI} </VirtualHost>
<VirtualHost *:443>
ServerName example.com
</VirtualHost>

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme