.htaccess Rewrite subdomain and directory

I am trying get the value of my subdomain, and insert it inbetween the folder and the filename of the values that are behind the domain for example,


Need to go to


I have managed to get the .html part working but wanted to know if there is any way I can get the sub domain and insert it in between the directory and file?

Could you please post the code you have so far?


What you’re trying to do will likely cause a loop (unless you’re very careful). It seems to be a senseless question (of little value) but …

RewriteEngine on
# capture subdomain value
RewriteCond %{HTTP_HOST} ^([a-z]+)\\.example\\.com$ [NC]
# capture file ... and exclude subdomain!!!
RewriteCond %{REQUEST_URI} ^directory/([a-z]+\\.[a-z]+)$
# Redirect
RewriteRule ^directory/%2$ directory/%1/%2 [R=301,L]
# I may have the order wrong on the % values but use the R=301 to see the redirection
# remove the R=301 after you're satisfied it works