Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create subdomain from a folder, but deny folder access

Okay, I've spent almost 4 hours searching the site for a solution, and another 2 hours searching google, wich is not quite useful when most sites copy paste the data. So here I decided to ask for a little help.

I created a subdomain successfully, let's say http://admin.myhost.com, wich basically take the data from the folder admin. The problem is that when I put http://myhost.com/admin the scripts are still accessible. Let's say I want to make this folder unaccessible, throwing a redirect to a custom file, for example index.php?template=error404 when some file is executed via the url http://myhost.com/admin. I've added this line to the .htaccess

RewriteRule ^admin(.*) /index.php?template=error404 [L]

and it ended up like this.

Options +FollowSymlinks

Options -Indexes

<FilesMatch "\.(tpl|ini|log)">
 Order deny,allow
 Deny from all
</FilesMatch>

RewriteEngine On

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?map=google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?map=google_base [L]
RewriteRule ^download(.*) /index.php?template=error404 [L]
RewriteRule ^admin(.*) /index.php?template=error404 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?seo_url=$1 [L,QSA]

It actually works only if I delete the empty .htaccess on the admin folder (It only says RewriteEngine on), but when I do that, the site http://admin.myhost.com throws an http error 500, and it is not accessible anymore. If I add again the .htaccess on the admin folder stating the RewriteEngine on rule, the subdomain starts to work again, but again, the folder is accessible via URL adding /admin

I can't seem to find the solution to this problem after several hours. Is there any way to do this? Am I doing something wrong?

like image 852
user3153340 Avatar asked Jan 24 '14 22:01

user3153340


Video Answer


1 Answers

In the /admin/.htaccess have this rule:

RewriteEngine On

# block access if HOSTNAME is not starting with admin.    
RewriteCond %{HTTP_HOST} !^admin\. [NC]
RewriteRule ^ - [F]

OR if you want to show /index.php?template=error404 then

RewriteCond %{HTTP_HOST} !^admin\. [NC]
RewriteRule ^ /index.php?template=error404 [L,QSA]
like image 181
anubhava Avatar answered Sep 28 '22 07:09

anubhava