Moaz  Mabrok Moaz Mabrok -3 years ago 110
Apache Configuration Question

.htaccess redirect from root( public_html ) to subdirectory and deny direct access to root

M1:
This works to redirect from root to subdirectory

# .htaccess main domain to subdirectory redirect
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subdirectory/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/index.html


M2:
This works to deny direct access to root

.htaccess in the root( public_html )

Order deny,allow
Deny from all


then using this allows direct access to the subdirectory

.htaccess in the subdirectory

Allow from all


website structure



public_html
|___ subdirectory ___ index.php
|___ .htaccess
|___ app
|___ something.php


when i add them both example.com gives

You don't have permission to access / on this server.

first time changing any thing in .htaccess

edit



what i want to do is block direct access to anything but the subdirectory.

also if

RewriteRule ^(.*)$ ?page=$1 [NC]


will conflict with it if added to the subdirectory

Answer Source

Have it this way:

RewriteEngine on 

# for example.com rewrite landing page to subdirectory/index.html
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^/?$ subdirectory/index.html [L]

# for example.com rewrite all other URIs to subdirectory/uri
# (?!subdirectory/) is a negative lookahead that stops rewrite then uri 
# already starts with /subdirectory/
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(?!subdirectory/)(.+)$ subdirectory/$1 [L,NC]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download