Say I've the next site:

http://example.com/site

that's exactly the same of

http://site.example.com

I'm wondering basically could obtain something similar to this

http://site.example.com/var/

using apache2's mod_rewrite.


Here's my try:

RewriteEngine on
RewriteRule ^([^/\.]+)$ /site/?var=$1

this works like no bodies business once the URL is http://example.com/site, consider I am only a beginner I'm not sure ways to get this to dedicate yourself http://site.example.com.

Any help could be really appreciated.

I have found an answer:

RewriteBase /

Some hosts require a RewriteBase specs to be able to work nicely. I really hope this can help.

Part one 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 might fail.
RewriteBase /
this line sets rewrite base. this directive uses when wish to set base URL for per-directory rewrites. default value is current physical path but generally, URL-base in In a roundabout way associated with physical filename pathways, therefore it is wrong to make use of default. for instance when utilizing virtual directory you need to set this method properly to mod_rewrite act well.

RewriteCond %% ^(www.)?example.com/site/(.*)
within this line first apache joins to variable (host address: www.example.com and ask for URI : /site/var ). it inspections result with pattern I gave. to begin result can be www. .after that needs to be example.com/site/ and also at the finish it may 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. since it has L flag it is the last rewrite rule apache inspections.%1 is everything marked by (.*) in rewrite condition.

RewriteRule ^([^/\.]+)$ /site/?var=$1 [L]


this lines are the road you devote your question: it rewrites every URL that does not have / and us dot with /site/?var=$1.