将多个路径指向同一个 - URL重写

时间:2015-11-25 18:08:01

标签: php apache .htaccess mod-rewrite url-rewriting

我开发了一个webapp,现在我想将同一个webapp实现到另一个客户端。使用相同的webapp,使用不同的路径进行维护变得很复杂:domain.com/client1和domain.com/client2。

所以,我的想法是使用URL重写以使用相同的webapp。例如。 domain.com/client1和domain.com/client2使用相同的webapp domain.com/webapplication,分别使用数据库client1和client2。

通过获取URL路径,我已经在我的php代码中识别出正确的数据库。我在.htaccess上做了很多测试但没有成功。 是否有可能做到这一点?如果是这样,有人可以给我一个想法或建议吗?我不想出于其他原因使用子域名。 提前谢谢。

编辑:数据库使用已完成。只是错过了我的网址重写。

2 个答案:

答案 0 :(得分:0)

包含此内容的.htaccess可能有所帮助:

AcceptPathInfo On
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?p=/$1 [QSA,L]
</IfModule>

然后可以在index.php中获取$ _GET ['p']的路径(放在文档根目录中)。

您的index.php可能如下所示:

if(preg_match($_GET['p'], "/^client1/")) {
    $database = "client1";
} elseif(preg_match($_GET['p'], "/^client2/")) {
    $database = "client2";
}
require_once 'webapplication/app.php';

但你可以使用像Alto Router这样的东西。

答案 1 :(得分:0)

非常有趣,其他人几乎在几个小时之前问了几个问题。无论如何,他的部分解释帮助我解决了我的问题。

https://stackoverflow.com/questions/33915314/use-htaccess-to-redirect-several-urls-to-one-folder

RewriteEngine on
RewriteRule ^name_1/(.*) store/$1

唯一的事情就是我必须为我的客户创建每个路径(文件夹),当然是空的。我肯定错过了。

相关问题