views:

18

answers:

2

Days later I asked about redirecting dynamic directories to index.php, and I got this code that works perfect (it's the only code I have in .htaccess):

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*) index.php

This translates urls like http://mydomain.example/dynamicdir/ to http://mydomain.example/index.php

Now I want to translate subdomains like http://dynamicdir.mydomain.example to http://mydomain.example/index.php?dir=dynamicdir

From examples I found in Internet I tried adding this line:

RewriteRule ^(.*)\.mydomain\.example index.php?dir=$1

But it doesn't work. I don't have enough experience with mod-rewrite to tell what's missing or wrong. Could you please help me to find a way to keep the dynamic directory translation, and add the catch-all subdomain rule?

Regards!

A: 

The mod_rewrite rules use the request path, which is relative to the virtual host. Try having different rewrite rules for each virtual host, but placing their documents in the same directory.

Borealid
A: 

With RewriteRule you can only test the URL path. For the host name, you need to use %{HTTP_HOST} in a RewriteCond:

RewriteCond %{HTTP_HOST} ^(.+)\.example\.example$
RewriteRule ^ index.php?dir=%1
Gumbo