多客户端应用程序的mod_rewrite表达式

时间:2012-03-21 09:48:16

标签: mod-rewrite url-rewriting url-routing

我有一个为多个客户提供服务的php应用程序。代码放在域的根目录中,并为所有客户共享。每个客户都可以使用查询字符串参数“id”访问它的页面。

我需要建议和示例代码如何通过mod_rewrite实现这种路由,或者通过php路由脚本实现它的更好方法:

首页

www.example.com/customerA --> www.example.com/customerA/main?id=1
www.example.com/customerB --> www.example.com/customerB/main?id=4

注意:“main”是main.php文件,不显示文件扩展名。 客户子文件夹不是真正的子文件夹。

内页正在使用其他参数,例如:

www.example.com/customerA/page1?id=1&par1=5

SERVER SIDE 上,所有重写都应解释为www.example.com/main?id=4 没有虚拟子文件夹。

感谢。

1 个答案:

答案 0 :(得分:0)

以下是应该起作用的:

RewriteRule ^/customerA /customerA/main?id=1 [QSA,NC,R=301,L]
RewriteRule ^/customerB /customerB/main?id=4 [QSA,NC,R=301,L]

RewriteRule ^/(customer(A|B))/main /main [QSA,NC]

现在我已经回答了你的问题,我很确定这不是你想要的。 如果你有很多客户,我已经回答了here一个关于电影的问题,但你是关于客户的,但原则是完全相同的。


如果你想更通用:

# if URL is not a real file...
RewriteCond %{SCRIPT_FILENAME} !-f
# if URL is not a real folder...
RewriteCond %{SCRIPT_FILENAME} !-d
# ...and if adding "php" points to a real file...
RewriteCond %{SCRIPT_FILENAME}.php -f
# ...then rewrite internally with "php" extension:
RewriteRule (.*) $1.php [QSA,NC]

希望这有帮助。