简体   繁体   中英

Prevent access to URL with htaccess with condition

I have an url :

https://abc.sitetest.com/images/abc/img.jpg

We read the subdomain from url which is abc . Using htaccess, we only allow the user to view abc and its subfolder. So if there's a folder /images/cde/... they can't view or read any file (not an image) in it.

It could be this but I'm not sure it's the way...

<Limit GET>
order deny,allow
deny from all
allow from abc.sitetest.com
</Limit>

Place this back reference based rule in your root .htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST}:%{REQUEST_URI} ^([^.]+)\.[^:]+:(?!.*?/\1/).* [NC]
RewriteRule ^ - [F]

This will allow:

https://abc.sitetest.com/images/abc/img.jpg
https://abc.sitetest.com/anything/abc/foo

but will block:

https://abc.sitetest.com/images/xyz/img.jpg
https://abc.sitetest.com/def/img.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM