1

我的 yii 项目具有以下结构:

webapp  
----frontend  
--------www  
------------index.php
----backend  
--------www  
------------index.php  
----api  
--------www  
------------index.php

https://github.com/tonydspaniard/yiinitializr-advanced

阿帕奇 2.2

每个 www 目录都有自己的 .htaccess 文件。例如“webapp/frontend/www/.htaccess”:

Options +FollowSymlinks

RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^/].*)$ /index.php/$1 [L]

对于应用程序的单独部分,我使用子域“api.webapp.com”、“admin.webapp.com”(public_html 文件夹中的符号链接):

webapp.com -> webapp/frontend/www   
api.webapp.com -> webapp/api/www  
admin.webapp.com -> webapp/backend/www

但是我对 api 的跨域 ajax 请求有问题,我想得到这样的东西:

http://webapp.com -> webapp/frontend/www/index.php  
http://webapp.com/api/ -> webapp/api/www/index.php   
http://webapp.com/admin/ -> webapp/backend/www/index.php   

将 URL 段路由到特定 Web 根目录的正确方法是什么?别名、.htaccess、符号链接,也许还有别的?

4

2 回答 2

1

多谢你们。现在我有两个
可行的解决方案:1)Alex Akr
webapp 是 root
webapp/.htaccess:

RewriteCond $1 ^api\/
RewriteRule ^(.*) api/www/index.php [L,PT]

RewriteCond $1 ^admin\/
RewriteRule ^(.*) admin/www/index.php [L,PT]

RewriteRule . frontend/www/index.php

2) webapp/frontend/www 是根目录。
在 apache 配置中设置别名:

Alias /api /var/www/webapp/data/www/webapp/api/www
<Directory /var/www/webapp/data/www/webapp/api/www>
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

在 /webapp/api/www/.htaccess

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule ^([^/].*)$ /api/index.php/$1 [L]
于 2013-08-05T06:40:25.147 回答
1

正如aCodeSmith所说,您可以正确配置.htaccess文件来解决问题。

  1. 的文档根目录http://webapp.com应该是webapp.
  2. 在文档根目录中,您应该.htaccess使用类似的路由规则创建文件。
RewriteCond $1 ^api\/
RewriteRule ^(.*) api/www/index.php [L,PT]

RewriteCond $1 ^admin\/
RewriteRule ^(.*) admin/www/index.php [L,PT]

重写规则。前端/www/index.php

希望,它会有所帮助。

于 2013-08-05T01:31:11.570 回答