Limiting access to one directory when on subdomain - apache

Howdy, I need to do the following:
allow access to sub.domain.com/directory from www.domain.com (linking to an asset)
prevent access to sub.domain.com/* (Return a 404 page when user hits subdomain directly
Is this possible using htaccess and, if so, any pointers on how to accomplish it?
Thanks

Using .htaccess you might want to look up htaccess and regex tutorials as they are of great help.
Stop sites from hotlinking is what I assume you are referring to therefore: you need to enable mod_rewrite and add this to the .htaccess file in the domain's root directory (with a little of what you learn):
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?domain\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://www.domain.com/noHotlink.gif
This should point you in the right direction towards how to do it.

Related

.htaccess redirect all files/folders after www.website.com/

Hi I am currently moving a site to new hosting, previously they had lots of folders/files in the document root which i have moved to another server i.e. www.website.com/foldera/test.zip or just www.website.com/file.mp3 now on hosting.website.com/file.mp3
because these url's are still in circulation i need to redirect all files to the new location so if the old link is clicked it will re-direct correctly.
They have a large amount of files/folders. Please advise on the best way to do this?
Thanks
Just add this to the htaccess file in the document root of your old website:
Redirect 301 / http://hosting.website.com/
Or, if you'd rather use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com$ [NC]
RewriteRule ^(.*)$ http://hosting.website.com/$1 [L,R=301]

htaccess rewrite rules for subdomain to fetch image directory from main domain

I am new to htaccess configuration but I have been reading and researching for awhile.
I have a mobile subdomain which I wish to fetch images located on my main domain. I am unsure as how to get it to work. My current code is:
RewriteCond %{HTTP_HOST} ^m\.\.website\.com [NC]
RewriteRule ^/avatar/(.*)$ http://www.website.com/avatar/$1 [P]
The image directory looks like this:
/avatar/thumbnail/image.jpg
/avatar/preview/image.jpg
/avatar/full/image.jpg
However, when I try to access m.website.com the images do not load.
Can anyone tell me what I am missing.
Any feedback would be greatly appreciated.
Remove leading slash from your rule and also take out extra DOT from host name condition:
RewriteCond %{HTTP_HOST} ^m\.website\.com [NC]
RewriteRule ^(avatar/.*)$ http://www.website.com/$1 [P,L]
This assumes you have mod_proxy enabled and working.

Protect folder on server only allow requests from allowed sites

Is there a way to not allow anyone to have access to the contents of a folder unless the were referred by a certain site? So if someone tried to load music.mp3 can I redirect them, but if example.com referred them allow them to see it. Would this be done through .htaccess?
Thanks!
something like
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www\.example\.com [NC]
RewriteRule \.jpg - [F]
should work
if its not http://example.com then give 403 status code

Redirecting PDF links from another domain using htaccess

We have two domains, let's call them first.com and second.com
We have a directory in second.com called reports, where all our PDFs are located, but we would like to these same PDFs accessible from first.com as well.
Can we redirect let's say first.com/reports/84839049.pdf to second.com/reports/84839049.pdf using htaccess?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain1.com/$1 [R=301, L]
Yes.
redirect /requested/url http://second.com/result/url
http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirect
You may want to consider using mod_rewrite though, unless you asked for an .htaccess configuration specifically because you have no access to the server configuration and mod_rewrite is disabled or not loaded.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://webdesign.about.com/od/mod_rewrite/qt/site_redirects.htm
You'll need some grasp of regex for mod_rewrite, but it can make configuration of the redirects a lot faster than having to add a redirect for every file on your site(s).

Setting subdomains using htaccess

I want to set a subdomain for all site users, like www.companyname.mydomain.com
I would like to use htaccess for this.
when somebody requests www.companyname.mydomain.com it should redirect to
myfile.php?name=companyname
How can I achieve this using an htaccess file ?
Thanks for the consideration.
Make sure this website is configured to respond to *.mydomain.com.
RewriteCond %{http_host} ^www.(\w+).mydomain.com [NC]
RewriteRule ^.*$ /myfile.php?name=%1 [L]
You might want to adjust ^.*$, since this check will rewrite regardless of what comes after www.companyname.mydomain.com.