使用 Apache mod_rewrite 相关问题到文件夹的子域
我想将我的所有子域映射到文件夹。
例如 subdomain.domain.com 应该显示 domain.com/clients/subdmain 的内容(透明地)
RewriteCond %{HTTP_HOST} ^(.+)\.oomail\.org
RewriteCond %{HTTP_HOST} !^www\.oomail\.org [NC]
RewriteRule ^(.+)$ /var/www/clients/%1/$1
我收到 500 内部服务器错误。为了检查问题所在,我附加了 [R] 标志,
RewriteRule ^(.+)$ /var/www/clients/%1/$1 [R]
我在地址栏中得到了这个结果
http://q1.oomail.org/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/always.html
然后我创建了一个details.php 文件,该文件转储 $_GET 变量详细信息。它的内容
<pre>
<?php
var_dump($_GET);
?>
</pre>
和修改RewriteRule为(注意:我将$1和%1发送到显示其内容的details.php中)
RewriteRule ^(.+)$ details.php?p=%1&d=$1
并在打开http://subdomain.oomail.org/file后.html
,我得到的输出为
array(2) {
["p"]=>
string(9) "subdomain"
["d"]=>
string(11) "details.php"
}
其中变量 p 是正确的(%1 的值),即“子域”,但是 d ($1 的值)是我期望的details.php我在 URL 中打开的 file.html 。我哪里出错了?或者如何将 file.html (URI) 提取到变量中进行映射?
I want to map all my subdomain to folder.
For example subdomain.domain.com should display content of domain.com/clients/subdmain (Transparently)
RewriteCond %{HTTP_HOST} ^(.+)\.oomail\.org
RewriteCond %{HTTP_HOST} !^www\.oomail\.org [NC]
RewriteRule ^(.+)$ /var/www/clients/%1/$1
I got 500 Internal Server Error. To check whats the problem I appended [R] flag
RewriteRule ^(.+)$ /var/www/clients/%1/$1 [R]
I got this result in address bar
http://q1.oomail.org/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/always.html
Then I created a details.php file which dumps the $_GET variable details. Its content is
<pre>
<?php
var_dump($_GET);
?>
</pre>
and modified RewriteRule to (Note: I am sending $1 and %1 into details.php which shows its content)
RewriteRule ^(.+)$ details.php?p=%1&d=$1
And after opening http://subdomain.oomail.org/file.html
, I got output as
array(2) {
["p"]=>
string(9) "subdomain"
["d"]=>
string(11) "details.php"
}
Of which variable p is correct (value of %1) which is "subdomain", BUT d (value of $1) is details.php which I was expecting to be file.html which I opened in URL. Where am I going wrong? or How can I fetch file.html (URI) into variable for mapping?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此规则:
在您的第一次尝试中,您遇到了无休止的重写循环,Apache 必须中断该循环(默认情况下不超过 10 次迭代)。在第二次尝试中,您面临有限的重写循环(我相信是两次迭代)。这就是 mod_rewrite 的工作原理,您应该在规则中计算这种可能性 - 特别是当您使用
.*
作为匹配模式时。我的规则中的第一个条件将防止重写循环发生(意味着规则仅在第一次迭代时有效)。不幸的是,这意味着一些限制:原始 URL 不能以
/clients/
开头。例如:此 URL 将不会被重定向:http://something.example.com/clients/hello-kitten.php
。解决方案 -- 将
clients
文件夹重命名为更独特的名称:例如_clients_folder_
(您必须更改该文件夹显然,重写规则也是如此)。需要阅读的内容:RewriteRule Last [L] 标志不起作用?
Use this rule:
In your first attempt you had endless rewrite loop which Apache had to interrupt (by default no more than 10 iterations). In second attempt you are facing limited rewrite loop (2 iterations, I believe). That's how mod_rewrite works and you should count such possibility in your rules -- especially when you use
.*
as matching pattern .The very first condition in my rule will prevent rewrite loop from happening (means rule will only work on very first iteration). Unfortunately this means some restrictions: the original URL CANNOT start with
/clients/
. For example: this URL will not be redirected:http://something.example.com/clients/hello-kitten.php
.Solution -- rename
clients
folder to something more unique: for example_clients_folder_
(you would have to change that folder in rewrite rules as well, obviously).Something to read: RewriteRule Last [L] flag not working?