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


Creating rewrite rules for 3 sub-domains using mod_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 和工作室,并且在它们的末尾并没有真正的顺序。

我最近的尝试是:

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

> 如果您不使用扩展.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]