I am having a scenario as following:
For example,I have a website http://www.example.com
, and I have setup a few subdomains such as http://video.example.com
, http://image1.example.com
, http://image2.example.com
. In Apache virtual host setting, they are using the same folder (e.g. /home/example/
). (these two domains have different bandwidth setup using mod_cband).
I have a subfolders /home/example/files/videos
, I want to make it only accessible from the subdomain http://video.example.com/files/videos/
but not from http://www.example.com/files/videos/
or any other subdomains.
How shall I configure this with a .htaccess
file?
Put this code in /home/mywebsite/files/videos/.htaccess
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /files/videos/
# if it is not for domain video.mywebsite.com then block it
RewriteCond %{HTTP_HOST} !^video\.mywebsite\.com$ [NC]
RewriteRule ^ - [F]
You can check the host and then handle this with mod_rewrite creating a 301 redirect in an .htaccess
file; although if you have access, it's much better if you do this in the httpd.conf
or an included config file instead.
Best Scenario
Since it looks like mod_cband wants a different virtualhost for each domain, you would setup your httpd.conf
file something like this and include the rewrite rules in the config itself. Some web hosts will do this method where the main account site is the DocumentRoot
and the other sites are all nested under it's directory:
<VirtualHost *:80>
ServerName www.mywebsite.com
DocumentRoot /home/mywebsite/
RewriteEngine on
RewriteRule ^/files/videos/(.*)$ http://video.mywebsite.com/$1 [R=301,L]
RewriteRule ^/files/images1/(.*)$ http://image1.mywebsite.com/$1 [R=301,L]
RewriteRule ^/files/images2/(.*)$ http://image2.mywebsite.com/$1 [R=301,L]
</VirtualHost>
<VirtualHost *:80>
ServerName video.mywebsite.com
DocumentRoot /home/mywebsite/files/video/
</VirtualHost>
<VirtualHost *:80>
ServerName image1.mywebsite.com
DocumentRoot /home/mywebsite/files/images1/
</VirtualHost>
<VirtualHost *:80>
ServerName image2.mywebsite.com
DocumentRoot /home/mywebsite/files/images2/
</VirtualHost>
Runner up
If you're using a hosting service provider where, you don't have access to the httpd.conf
files, and they haven't setup the domains as an alias
of the main domain (each domain has a separate folder), then you would write your rules in the root .htaccess
for www.mywebsite.com
like this:
RewriteEngine On
RewriteRule ^(files/videos/.*)$ http://video.mywebsite.com/$1 [R=301,L]
RewriteRule ^(files/images1/.*)$ http://image1.mywebsite.com/$1 [R=301,L]
RewriteRule ^(files/images2/.*)$ http://image2.mywebsite.com/$1 [R=301,L]
Most overhead
If they are using aliases (where everything has the exact same document root) then you'll need to check the requested hostname with a .htaccess
file that's commonly enacted by all:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^video.mywebsite.com$
RewriteRule ^(files/videos/.*)$ http://video.mywebsite.com/$1 [R=301,L]
#Check to make sure if they're on the video domain
#that they're in the video folder otherwise 301 to www
RewriteCond %{HTTP_HOST} ^video.mywebsite.com$
RewriteCond %{REQUEST_URI} !^/files/videos [NC]
RewriteRule ^.*$ http://www.mywebsite.com/ [R=301,L]
RewriteCond %{HTTP_HOST} !^image1.mywebsite.com$
RewriteRule ^(files/images1/.*)$ http://image1.mywebsite.com/$1 [R=301,L]
#Check to make sure if they're on the image1 domain
#that they're in the images1 folder
RewriteCond %{HTTP_HOST} ^image1.mywebsite.com$
RewriteCond %{REQUEST_URI} !^/files/images1 [NC]
RewriteRule ^.*$ http://www.mywebsite.com/ [R=301,L]
RewriteCond %{HTTP_HOST} !^image2.mywebsite.com$
RewriteRule ^(files/images2/.*)$ http://image2.mywebsite.com/$1 [R=301,L]
#Check to make sure if they're on the image1 domain
#that they're in the images2 folder
RewriteCond %{HTTP_HOST} ^image2.mywebsite.com$
RewriteCond %{REQUEST_URI} !^/files/images2 [NC]
RewriteRule ^.*$ http://www.mywebsite.com/ [R=301,L]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With