使用mod_rewrite为3个子域创建重写规则

时间:2012-06-22 03:42:25

标签: php .htaccess mod-rewrite rewrite

我正在使用CodeIgniter创建一个PHP应用程序,这个项目实际上有3个子应用程序,因此每个应用程序有三个单独的索引文件。我打算为这些应用程序使用三个不同的子域。最后,该项目位于我的Apache文档根目录下的子目录下。

例如,此用户URL应映射到

http://app1.example.com/ABC -----> http://app1.example.com/ext/app1.php/ABC
http://app2.example.com/DEF -----> http://app2.example.com/ext/app2.php/DEF
http://app3.example.com/XYZ -----> http://app3.example.com/ext/app3.php/XYZ

还有app4和app5等其他应用,我不想将规则应用到。

之前我曾经使用过并写过重写规则,但它们相当简单,我无法破解这一规则。我在.htaccess中使用了以下内容,当我尝试访问时说app1,网址变为http://app1.example.com/ext/app1.php/ext/app1.php/ext/app1.php/{manytimes}

所以它正在循环中。

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^app1\.example\.com$
RewriteRule ^ http://app1.example.com/example/app1.php%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^app2\.example\.com$
RewriteRule ^ http://app2.example.com/example/app2.php%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^app3\.example\.com$
RewriteRule ^ http://app3.example.com/example/app3.php%{REQUEST_URI} [L]

所以真正的子域名是 - api,viewer和studio,并且最后没有序列。

我最近的尝试是:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(viewer|api|studio)\.example\.com$
RewriteRule ^/(.*)\.example\.com/(.*) http://$1.example.com/ext/$1.php/$2 [L]

1 个答案:

答案 0 :(得分:0)

如果您不使用扩展程序.php,您应该可以执行以下操作:

RewriteEngine On
RewriteBase /

# For app1 through app3.example.com
RewriteCond %{HTTP_HOST} ^(viewer|api|studio)\.example\.com$
# If the request path does not contain the string (viewer or api or studio).php
RewriteCond %{REQUEST_URI} ! %1\.php
# Re-write it to go to the appropriate app
RewriteRule ^.*$ /ext/%1.php%{REQUEST_URI} [L]