douan3182 2013-10-04 12:57
浏览 67

Apache Alias和多个版本的代码库

I am trying to run a versioned API from separate directories outside my Apache doc root.

My current approach is to try this with the Alias directive:

Alias /api/v1.2/ /var/www/api-v1.2/
Alias /api/v1.1/ /var/www/api-v1.1/

This is working fine, however I am using a PHP framework (Codeigniter) that uses mod_rewrite to route all requests to my index.php front controller:

RewriteCond %{REQUEST_URI} !=/server-status
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/?$1 [L]

I can access actual files via the URL, and the alias directive works fine. When I access a URL that the system means to rewrite, the request is served from the doc root.

How can I get my CI application to follow the Alias rules while still routing traffic to each respective front controller?

EDIT: to be clear, I have 3 separate versions of my CI codebase: 1 in the Apache doc root, and 2 others in each aliased directory. I want to route requests to the correct version of the codebase based on the URL (defaulting to the doc root if no Alias is matched).

/var/www/html (doc root)
/var/www/api-v1.2
/var/www/api-v1.1
  • 写回答

2条回答 默认 最新

  • doumou3883 2013-10-04 13:19
    关注

    So, the problem is that you aren't preventing your initial .htaccess from rewriting the folders.

    You will need 3 .htaccess files, one in the root and one in each of the v1.1 and v1.2 folders.

    Root (docroot):

    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_URI} !=/server-status
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|robots\.txt|api/v1.1|api/v1.2)
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    The important line being the last RewriteCond where it tells apache to ignore the api folders.

    api-v1.1 folder:

    RewriteEngine on
    RewriteBase /api/v1.1
    RewriteCond %{REQUEST_URI} !=/server-status
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|robots\.txt)
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    api-v1.2 folder:

    RewriteEngine on
    RewriteBase /api/v1.2
    RewriteCond %{REQUEST_URI} !=/server-status
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|robots\.txt)
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    The important lines being RewriteBase. This tells apache the directory we're working in and will process the rewrites based on its designation.

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line