.htaccess将[虚拟]子域重写为[虚拟]文件夹

时间:2015-06-24 01:57:33

标签: php apache .htaccess mod-rewrite

我有一个包含虚拟页面的站点,其中任何请求都以这种方式重写到索引页面:

RewriteEngine On  
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  
RewriteRule ^.*$ ./index.php

即,http://www.example.com/job/edit被重写为http://www.example.com/index.php/job/edit,我在其中解析它。这很有效。

现在,我需要处理每个虚拟子域,以便http://user1.example.com/job/edit成为http://www.example.com/index.php/user1/job/edit

我在CPanel中添加了一个通配符子域(*.example.com),因此每个子域都指向域文件夹(没有它我在每个子域都找不到服务器'。

我尝试了我在这里找到的每一个例子,但我无法让它们发挥作用。如果我在索引页面上打印出$_SERVER['REQUEST_URI'],我总是得到最后一部分,但不是子域名(即/job/edit)。

有没有办法做到这一点?

提前致谢。

修改:我已经有了解决方法。 $_SERVER['HTTP_HOST'] var包含整个域,即user1.example.com。我可以在PHP中解析它并提取子域,但我认为它应该是一种更优雅的方式。

1 个答案:

答案 0 :(得分:0)

我已经完成了这样的事情。

创建一个名为“user”的文件夹

创建一个index.php来处理对用户函数的调用

在路由

的Httpd.conf中添加虚拟主机

* .example.com到Path / public_html / user

<VirtualHost 0.0.0.0>
ServerAlias *.example.com
ServerAdmin webmaster@yourdomain.com
DocumentRoot /home/yourdoma/public_html/user
ServerName yourdomain.com
User yourdoma
Group yourdoma
BytesLog /usr/local/apache/domlogs/yourdomain.com-bytes_log
CustomLog /usr/local/apache/domlogs/yourdomain.com combined
ScriptAlias /cgi-bin/ /home/yourdoma/public_html/joe/cgi-bin/
</VirtualHost>

现在所有呼叫都将路由到用户文件夹。

在用户文件夹中添加Htaccess文件以重写基本路径

相关问题