jmginer
jmginer

Reputation: 381

RewriteCond if url is not "cdn.domain.com/images/"

we have created a CDN subdomain to host images, using this URL:

https://cdn.example.com/images/

What we want is to redirect, if someone goes to any other path of this subdomain. For example:

https://cdn.example.com/
https://cdn.example.com/blabla/

Redirect to other domain, just not redirect if the folder is images.

Upvotes: 1

Views: 355

Answers (2)

MrWhite
MrWhite

Reputation: 45829

Try the following using mod_rewrite, near the top of your .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^cdn.example.com [NC]
RewriteRule !^images/ https://example.com%{REQUEST_URI} [R,L]

If you access cdn.example.com/<something> and the URL-path does not start /images/ then redirect to https://example.com/<something> (the main domain).

Change R to R=301 (after you've made sure it is working OK) if you want a permanent redirect.


UPDATE: To exclude additional folders (eg. images2 and images3) then you can change the RewriteRule directive to:

RewriteRule !^(images|images2|images3)/ https://example.com%{REQUEST_URI} [R,L]

If the folders are literally called images, images2 and images3 then this could be simplified to match (or rather not match) any URL of the form /imagesN - where N is an optional digit:

RewriteRule !^images\d?/ https://example.com%{REQUEST_URI} [R,L]

Upvotes: 1

Abdul Rafay
Abdul Rafay

Reputation: 807

create htaccess file in your document root, and deny direct access to all files

Order deny,allow
Deny from all

and create htaccess file in images:

Allow from all

Upvotes: 1

Related Questions