【问题标题】:How to route specific URL segments to separate web root directories?如何将特定的 URL 段路由到单独的 Web 根目录?
【发布时间】:2013-07-31 13:00:20
【问题描述】:

我的 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、符号链接,也许还有别的?

【问题讨论】:

  • .htaccess 和多个 index.php 文件最适合将整个 uri 段指向子文件夹。

标签: apache .htaccess mod-rewrite yii


【解决方案1】:

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

  1. @987654321@ 的文档根目录应该是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

希望,它会有所帮助。

【讨论】:

    【解决方案2】:

    谢谢,伙计们。 现在我有两个可行的解决方案:
    1) 亚历克斯·阿克尔
    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 是 root。
    在 apache config 中设置别名:

    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]
    

    【讨论】:

      猜你喜欢
      • 2016-04-16
      • 1970-01-01
      • 2014-08-19
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 2016-06-12
      相关资源
      最近更新 更多