Subfolder to Subdomain with .htaccess

Convert Subfolder to Subdomain with .htaccess

For Example, if you have your url is “” and “” but you want to convert them all to subdomain too.

such as “” and “”.

The easiest way to convert folder to subdomain is to use .htaccess, Reconfigured your ServerAlias in Apache Virtual host and add a wildcard A record into your dns record.

Step 1 – Make the .htaccess look like this

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . – [L]

RewriteCond %{HTTP_HOST} !
RewriteCond %{HTTP_HOST} !^
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.domain\.com(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L,QSA]

Step 2 – Add this to your Configured of ServerAlias in Apache Virtual Host

ServerAlias *

Step 3 – Add A DNS Record

* IN A 123.456.789.000(your IP)

Note: Change to your domain! 

I hope this help!

Leave me a comment if you have any question.