RewriteRule for handling multiple domains within one single app?

孤街醉人 提交于 2019-12-10 11:45:28

问题


Say I have an app at example.com.

One customer migth have it's content in example.com/customer/hisname/.

Well, I would like to make possible that the customer assigns a domain for his content, say hisname.com, so this (hisdomain.com) renders example.com/customer/hisname/.

To do that I have created two virtual hosts, both having the root directory in the same place, and I have written a .htaccess file in order to filter the http_host and if not example.com then rewrite in such manner that it passes the request to example.com/customer/hisname/.

Still, this approach doesn't work while I do not get the /customer/hisname/ part in URI. If I go to hisname.com/list/ in the example.com/index.php, where I would need to have both /list/ and (as a prefix) /customer/hisname/, I only get, as you might expect just /list/.

So, what would be the right approach for this?


回答1:


If you can create Virtual Hosts, you should set the proper document root instead of using mod_rewrite

<VirtualHost w.x.y.z:80>
    ServerAdmin webmaster@hisname.com
    DocumentRoot /path/to/www/customer/hisname
    ServerName hisname.com
    ...
</VirtualHost>

If this is not possible, you can extract the host part and do an internal rewrite

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(?www\.)?example\.com$
RewriteCond %{HTTP_HOST} ^(.+)\.com$
RewriteRule ^ /index.php?prefix=customer/%1&request=%{REQUEST_URI} [L,QSA]



回答2:


use the variable $_SERVER['HTTP_HOST'] and load a different view depending on the host requested. Make sure you handle all subhosts needed and have a default page as well



来源:https://stackoverflow.com/questions/15729641/rewriterule-for-handling-multiple-domains-within-one-single-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!