Say I have the following site:
that is the same of
I was wondering if I could obtain something like this
using apache2’s mod_rewrite.
Here’s my try:
RewriteEngine on
RewriteRule ^([^/\.]+)$ /site/?var=$1
this works like a charm when the URL is http://example.com/site, but since I’m just a beginner I don’t know how to get this to work for http://site.example.com.
Any help would be really appreciated.
The first part redirect
example.com/site/vartosite.example.com/var.Second rewrite rule, rewrite your url in site.example.com,
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.comand request URI :/site/var). then it checks result with pattern I gave. first of result can bewww..after that should be
example.com/site/and at the end it can be anything.this line redirects user request to
http://site.example.com/%1with 301 status code. because it hasLflag it’s the last rewrite rule apache checks.%1is everything marked by(.*)in rewrite condition.this line is the line you put in your question: it rewrites every URL that doesn’t have
/and dot with/site/?var=$1.