douqian6315 2015-06-24 01:57
浏览 36

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

I have a site with virtual pages where any request is rewrote to the index page in this way:

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

I.e, http://www.example.com/job/edit is rewrote as http://www.example.com/index.php/job/edit where I parse it. That works great.

Now, I need to process every virtual subdomain in order that i.e. http://user1.example.com/job/edit becomes http://www.example.com/index.php/user1/job/edit.

I added in CPanel a wildcard subdomain (*.example.com), so every subdomain is pointed to the domain folder (without it I got 'Server not found' on every subdomain).

I tried every example that I found here, but I can't get them to work. If I print out $_SERVER['REQUEST_URI'] on the index page, I allways get the last part, but not the subdomain (i.e. /job/edit).

Is there any way of doing this?

Thanks in advance.

Edit: I already have a workaround for doing it. The $_SERVER['HTTP_HOST'] var contains the whole domain, i.e. user1.example.com. I can parse it in PHP and extract the subdomain, but I think it should be a more elegant way of doing it.

  • 写回答

1条回答 默认 最新

  • donglei1699 2015-06-24 02:39
    关注

    I have Done Same Like this.

    Create a Folder called "user"

    Create A index.php to handle The calls To user functions

    Add A Virtual host in the Httpd.conf That Route The

    *.example.com to the 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>
    

    Now all Call will route to user Folder.

    Add a Htaccess file in user folder to rewrite the base path

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制