几天后,我询问有关将动态目录重定向到index.php的问题,我得到了这段运行完美的代码(这是我在.htaccess中唯一的代码):
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*) index.php
这会翻译像 http://mydomain.example/dynamicdir/ 到 http://mydomain.example/index.php
现在我想翻译 http://dynamicdir.mydomain.example 到 http://mydomain.example/index.php?dir=dynamicdir
从我在互联网上找到的示例中,我尝试添加这一行:
RewriteRule ^(.*)\.mydomain\.example index.php?dir=$1
但它不起作用。我没有足够的 mod-rewrite 经验来判断缺少或错误的内容。您能帮我找到一种保留动态目录翻译并添加捕获所有子域规则的方法吗?
问候!
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!
发布评论
评论(2)
mod_rewrite 规则使用请求路径,该路径相对于虚拟主机。尝试为每个虚拟主机设置不同的重写规则,但将它们的文档放在同一目录中。
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.
使用
RewriteRule
你只能测试URL 路径。对于主机名,您需要在 %{HTTP_HOST} ="nofollow noreferrer">RewriteCond
:With
RewriteRule
you can only test the URL path. For the host name, you need to use%{HTTP_HOST}
in aRewriteCond
: