Subfolder to Subdomain with .htaccess

Convert Subfolder to Subdomain with .htaccess

For Example, if you have your url is “http://domain.com/sexy/” and “http://domain.com/files” but you want to convert them all to subdomain too.

such as “http://sexy.domain.com” and “http://files.domain.com”.

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} !www.domain.com
RewriteCond %{HTTP_HOST} !^domain.com
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 *.domain.com

Step 3 – Add A DNS Record

*.domain.com. IN A 123.456.789.000(your IP)

Note: Change domain.com to your domain! 

I hope this help!

Leave me a comment if you have any question.

Cheers!