Search code examples
phpapache.htaccess

subdomain to sub folder mapping


e.g. my subdomain is abc.example.com

my folder is /htdocs/abcfiles

my requirement is when i accessing the url http://abc.example.com. it will display the webpage from "abcfiles/" folder.

my .htaccess file

RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc.example.com$
RewriteCond %{REQUEST_URI} !^/abcfiles/
RewriteRule (.*) /abcfiles/$1 [L]
RewriteBase /abcfiles/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /abcfiles/index.php [L]

Its working but "/abcfiles/" showing in some links. i.e abc.example.com/abcfiles/...


Solution

  • You can have this code in site root .htaccess:

    RewriteEngine On
    
    # if /abcfiles/ is in URL then remove it
    RewriteCond %{THE_REQUEST} \s/abcfiles/(\S*) [NC]
    RewriteRule ^ /%1 [L,NE,R=301]
    
    # if hostname starts with abc. then route the request to abcfiles/
    RewriteCond %{HTTP_HOST} ^abc\. [NC]
    RewriteCond %{REQUEST_URI} !^/abcfiles/ [NC]
    RewriteRule (.*) abcfiles/$1 [L]
    

    Have this code in abcfiles/.htaccess:

    RewriteEngine On
    
    RewriteRule ^index\.php$ - [L,NC]
    
    # route every request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]