RewriteRule for handling multiple domains within o

2019-05-31 04:08发布

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?

2条回答
地球回转人心会变
2楼-- · 2019-05-31 04:49

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]
查看更多
对你真心纯属浪费
3楼-- · 2019-05-31 04:58

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

查看更多
登录 后发表回答