简体   繁体   中英

Creating rewrite rules for 3 sub-domains using mod_rewrite

I am creating a PHP app using CodeIgniter and this project actually has 3 sub apps, so there are three separate index files for each of the app. I intend to use three different sub-domains for these apps. Lastly the project is under a sub directory in my Apache document root.

So for example, this User URL should map to

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

There are other apps like app4 and app5, that I do not want to apply the rule to.

I have used and written rewrite rules before but they were rather simpler and I am not able to crack this one. I used the following in my .htaccess and when I try to access lets say app1, the url becomes http://app1.example.com/ext/app1.php/ext/app1.php/ext/app1.php/{manytimes} .

So it's going in a loop.

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]

So the real subdomains are - api, viewer and studio and do not really have sequence at the end of them.

My latest attempt was:

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

If you will not be using the extension .php you should be able to do something like this:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM