Eric_WVGG
Eric_WVGG

Reputation: 2947

htaccess redirect non-image resources

I have a website somedomain.com, and a CDN set up to the subdomain www.somedomain.com. (I would have liked to make it cdn.somedomain.com, but did not have the option).

The CDN is effectively a dumb mirror. I want to use it to deliver image assets, but NOT pages.

These .htaccess rule are doing the right job of redirecting image requests to the CDN. (I don't really care about restricting by file extension.)

RewriteCond %{HTTP_HOST} ^somedomain\.com$ [NC] RewriteRule ^images\/?(.*)$ "http\:\/\/www\.somedomain\.com\/images\/$1" [R=301,L]

How can I extend this same .htaccess file to ensure that non-asset requests like www.somedomain.com/something/or/other is redirected to somedomain.com/something/or/other (no www)?

Upvotes: 0

Views: 74

Answers (1)

Panama Jack
Panama Jack

Reputation: 24448

You should be able to do this. Use two rules. And you don't need to do escapes in the RewriteRule.

RewriteCond %{HTTP_HOST} ^somedomain\.com$ [NC]
RewriteRule ^images/([^/]+)$ http://www.somedomain.com/images/$1 [R=301,L]

#if it's not the image folder redirect to main domain without www
RewriteCond %{HTTP_HOST} ^www\.somedomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/images [NC]
RewriteRule ^(.*)$ http://somedomain.com/$1 [R=301,L]

Upvotes: 2

Related Questions