Blogging / Computer projects / Networking
Use .htaccess URL rewriting to bind a domain to a subdirectory and create a subsite
Apache hosting generally supports .htaccess URL rewriting, which lets you map a domain to a subdirectory and host multiple sites under one account. Example: map htaccess.86k.xyz to the htaccess directory.
Contents of .htaccess in the Root Directory
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Bind htaccess.86k.xyz to the htaccess subdirectory
RewriteCond %{HTTP_HOST} ^htaccess\.86k\.xyz$ [NC]
RewriteCond %{REQUEST_URI} !^/htaccess/
RewriteRule ^(.*)$ htaccess/$1?Rewrite [L,QSA]
#You can map multiple domains; just repeat the three lines above and change the domain and directory names.
</IfModule>
Even without escaping the “.” in HTTP_HOST with a backslash, htaccess.86k.xyz will still match, because “.” matches any single character. Contents of .htaccess in the htaccess Directory
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Allow only the mapped domain; redirect other domains with a 301.
RewriteCond %{HTTP_HOST} !^htaccess\.86k\.xyz$ [NC]
RewriteRule (.*) http://htaccess.86k.xyz/$1 [L,R=301]
#Handle the htaccess directory under the bound directory
RewriteCond %{REQUEST_URI} ^\/htaccess\/ [NC]
RewriteCond %{QUERY_STRING} !^(.*)?Rewrite
#htaccess.800m.net/htaccess actually accesses htaccess.86k.xyz/htaccess/htaccess
RewriteRule ^(.*)$ /%{REQUEST_URI}/%{REQUEST_URI}/$1?Rewrite [L,QSA]
</IfModule>
For a simple mapping, you can leave out the .htaccess file inside the subdirectory.
Reposted from: crxis
Published 05/26/2018 · Krunk Zhou
Join the conversation