3

I'd like to map subdomain.example.com to www.example.com/subdomain using an internal URL rewrite that looks at the host name and simply forwards any request to a subdirectory with the same name as the subdomain.

Thanks for your help

Gumbo
  • 620,600
  • 104
  • 758
  • 828
krisdyson
  • 3,177
  • 7
  • 42
  • 85

2 Answers2

1

Using .htaccess:

RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com 
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
pritaeas
  • 2,073
  • 5
  • 36
  • 48
  • And how does this answer the question? – Gumbo Aug 05 '10 at 09:17
  • Assuming you use Apache, you can use this .htaccess file to do the redirects for you. Ow, have I misread the mapping part ? – pritaeas Aug 05 '10 at 09:18
  • Sorry yes it's Apache. This answer pointed me in the right direction, thank you. – krisdyson Aug 05 '10 at 09:47
  • 1
    This answer redirects the user's browser to the said page. The question was about internally rewriting the request to a subdirectory that is intended to host the subdomain. – jorisw Mar 22 '13 at 16:23
1

See the following for subdomain part if you are on Apache:

  1. You need to create a wildcard domain on your DNS server *.website.com
  2. Then in your vhost container you will need to specify the wildcard aswell *.website.com - This is done in the ServerAlias http://httpd.apache.org/docs/1.3/mod/core.html#serveralias

Then you will want to use a rewrite rule similar to the one posted by pritaeas or get the domain with you PHP script and redirect based upon it.

$url = substr($_SERVER['SERVER_NAME'], 0, strpos($_SERVER['SERVER_NAME'], '.'));
header("Location: http://mydomain.com/$url");
Community
  • 1
  • 1
Treffynnon
  • 20,865
  • 5
  • 62
  • 96