The first part redirect example.com/site/var
to site.example.com/var
.
Second rewrite rule, rewrite your url in site.example.com,
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^(www\.)?example.com/site/(.*)
RewriteRule (.*) http://site.example.com/%1 [R=301,L]
RewriteRule ^([^/\.]+)$ /site/?var=$1 [L]
Explanation:
RewriteEngine on
this line turns rewrite engine on. otherwise it may fail to work.
RewriteBase /
this line sets rewrite base. this directive uses when want to set base URL for per-directory rewrites. default value is current physical path but in most cases, URL-base in NOT directly related to physical filename paths, so it's wrong to use default. for example when using virtual directory you should set this option correctly to mod_rewrite act well.
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^(www.)?example.com/site/(.*)
in this line first apache joins to variable (host address: www.example.com
and request URI : /site/var
). then it checks result with pattern I gave. first of result can be www.
.after that should be example.com/site/
and at the end it can be anything.
RewriteRule (.*) http://site.example.com/%1 [R=301,L]
this line redirects user request to http://site.example.com/%1
with 301 status code. because it has L
flag it's the last rewrite rule apache checks.%1
is everything marked by (.*)
in rewrite condition.
RewriteRule ^([^/\.]+)$ /site/?var=$1 [L]
this line is the line you put in your question: it rewrites every URL that doesn't have /
and dot with /site/?var=$1
.