Forum Moderators: phranque

Message Too Old, No Replies

Apache mod rewrite - subdomain to folder

         

leofire

2:24 pm on Jun 10, 2008 (gmt 0)

10+ Year Member



Hi to everybody,

I would like to redirect all subdomain to related folder with 302 redirect. I have complete access to web server (root account) and I can modify everithyng into Apache config and .htaccess.
So i need to do it:
[xyz.example.com...] ===> http://www.example.com/xyz/

where "xyz" is everything but not www.
How can do that ?

At the moment I can do only this:
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Redirect 301 from example.com to www.example.com.
Can anyone help me please ?
Thanks and good day to everybody,

Leonardo

[edited by: jdMorgan at 11:24 pm (utc) on June 10, 2008]
[edit reason] example.com [/edit]

leofire

4:08 pm on Jun 10, 2008 (gmt 0)

10+ Year Member



I have correctly set the DNS in order to ping all subdomain...but with this rule:
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+).example.com
RewriteCond /REALPATH/www.example.com/%1 -d
RewriteRule ^(.*) /%1/$1 [R=301,L]

still nothing....

[edited by: jdMorgan at 11:26 pm (utc) on June 10, 2008]
[edit reason] example.com [/edit]

leofire

5:48 pm on Jun 10, 2008 (gmt 0)

10+ Year Member



Solved!
Add this line into the zone file of that domain;
www IN A IPADDRESS
*.example.com. IN CNAME www

Where IPADDRESS is the IP of your server.
Into the Virtual Host add this lines:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+).example.com
RewriteRule ^(.*) http://www.example.com/%1/ [R=302,L]

Now everything works fine.
Thanks,

Leonardo

[edited by: jdMorgan at 11:25 pm (utc) on June 10, 2008]
[edit reason] example.com [/edit]

jdMorgan

11:18 pm on Jun 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why do you want to 'expose' the subdirectory used to host the subdomain's files? I'd recommend an internal rewrite, not an external redirect:
In httpd.conf or conf.d:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com
RewriteRule ^/(.*)$ /%1/ [L]

Note also the correction to the pattern to prevent a double leading slash on the rewritten URL.

Jim