1

Say I have the following site:

http://example.com/site

that is the same of

http://site.example.com

I was wondering if I could obtain something like this

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

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.

Simone
  • 20,302
  • 14
  • 79
  • 103
  • where did you put .htacess file? in site directory? – undone Sep 30 '11 at 05:15
  • yes, I've put it into mydomain.com/html/site that is the site directory – Simone Sep 30 '11 at 10:47
  • I confused! are you sure your rewrite rule works for example.com/site?? because it seems to me it should work for site.example.com!!! – undone Sep 30 '11 at 11:30
  • It works now with `RewriteBase /`, but I still can't do a redirect from **example.com/site** to **site.example.com** – Simone Sep 30 '11 at 19:13
  • Okay, as I understand you want to redirect (external) example.com/site to site.example.com! if I undertood well, tell me to put the code here! – undone Sep 30 '11 at 23:09
  • If my code doesn't work, can you give me a messenger ID( like yahoo) that I can answer faster? – undone Oct 01 '11 at 13:46

2 Answers2

3

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.

undone
  • 7,857
  • 4
  • 44
  • 69
  • It works! Thank you! And.. could you please explain to me briefly your code? The bounty will be yours :) – Simone Oct 01 '11 at 14:48
  • just figured it doesn't work if I type **www**.example.com/site ... how can I resolve? – Simone Oct 01 '11 at 14:54
1

I've found a solution:

RewriteBase /

Some hosts need a RewriteBase specification in order to work well. I hope this helps.

Simone
  • 20,302
  • 14
  • 79
  • 103