开发者

RewriteEngine: subdomain to index.php, how?

开发者 https://www.devze.com 2023-01-06 04:32 出处:网络
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):

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.exampl开发者_StackOverflow社区e/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!


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消