【问题标题】:how to Mod-rewrite php + Slim url to sub-directory如何修改 php + Slim url 到子目录
【发布时间】:2015-07-11 20:50:03
【问题描述】:

我正在使用 Slim 2.4 和 php 5.3。我在 localhost 中的目录结构是这样的 :

  1. Web 根目录
    • 应用名称
      • API
        • v1
          • 包括
          • 超薄
          • 工具
          • .htaccess (#1)
          • index.php (#1)
      • 资产
        • CSS
        • js
        • img
      • .htaccess (#2)
      • index.php (#2)

我想访问将打开 localhost/AppName/API/v1localhost/AppName/v1。由于进一步的参数被忽略,这部分工作。当我尝试获取 localhost/AppName/v1/anyOtherVariable 时,它只会打开 localhost/AppName/API/v1

现在,#1 index.php 包含所有 Slim 代码(路由和东西),但 #1 .htaccess 是空白的。 #2 index.php 包含一个简单的静态主页,其中包含一些链接。在#2 .htaccess 我写过

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^v1(/.*)?$ API/v1/index.php$1 [QSA,L]
</IfModule>

我真的没办法了。

【问题讨论】:

    标签: php .htaccess mod-rewrite slim


    【解决方案1】:

    一个快速而肮脏的解决方案是定义一个版本常量,然后将其从全局变量 $_SERVER['REQUEST_URI'] 中删除。 例如我的 api url 是http://localhost/api/v1/hello/firstname/lastname

    我定义了一个常量,然后将其从 $_SERVER['REQUEST_URI'] 中删除,如下所示:

    define('API_VERSION', '/api/v1');
    
    $_SERVER['REQUEST_URI'] = str_replace(API_VERSION, '', $_SERVER['REQUEST_URI']);
    
    $application->get (
        '/hello/:firstname/:lastname',
         ....
    

    它就像一个魅力:)

    【讨论】:

    • 我的 .htaccess 是:RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L] .
    【解决方案2】:

    在敲了 100 次 之后,我决定改变我的目录结构,现在一切正常。 :) 我发布这个答案是为了帮助其他遇到同样问题的人。这是我的目录结构。

    • WebRoot /
      • 应用名称/
        • v0.2/
        • v0.5/
        • v0.9 /
          • _APP /
            • 缓存/
            • 数据/
            • 库/
            • 供应商/
              • 苗条/
              • 用户蛋糕/
              • MeekroDB /
          • Acc /(单独的路线)
          • API /
            • .htaccess
            • index.php
          • 资产 /
            • 图片/
            • CSS /
            • js/
            • 字体/
          • 测试/
          • .htaccess
          • 404.php
          • config.php
          • index.php

    现在,这些是我在主文件中使用的代码: API 下的 index.php 包含 Slim Routes。

    APi 下的.htaccess 包含以下代码:

    <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^ index.php [QSA]
    </IfModule>
    

    v0.9目录下的index.php包含主页

    同一目录下的.htaccess包含:

    <IfModule mod_rewrite.c>
    RewriteEngine on
    
    
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^ index.php [QSA,L] 
    
    
    RewriteRule ^(_APP/) - [F,L,NC]
    </IfModule>
    

    此 .htaccess 限制对 _APP 文件夹的直接访问,因为它包含敏感数据。现在人们会告诉我将其移到可公开访问的目录之外,但对于像我这样使用共享主机的用户,托管公司会限制到公共根目录之外。尽管使用此设置可以保护应用程序,但您应该始终尝试将此 _APP 目录移到公共目录之外。

    主要的 config.php 包含:

    <?php
    // PROJECT SETTINGS
    // Show Erros on Localhost and Local Ethernet only
    $test_server = $_SERVER['SERVER_NAME'] == "127.0.0.1" || $_SERVER['SERVER_NAME'] == "localhost" || substr($_SERVER['SERVER_NAME'],0,3) == "192";
    ini_set('display_errors',$test_server);
    error_reporting(E_ALL|E_STRICT);
    
    $_APP=array();
    
    // META CONFIGURATIONS
    $_APP= array_merge($_APP, array(
            'meta'=>array(
                'name'=>'expoWWW',
                'ver'=>'v 0.0.2',
                'url'=>'http://localhost/AppName/v0.9/',
                'root'=>$_SERVER['DOCUMENT_ROOT'].'/',
                'appUrl'=>'AppName/v0.9/',
                'author'=>'Abhishek Deb (iam@debabhishek.com)',
                'admin'=>''
                )
            ));
    
    
    // DATABASE CONFIGURATION
    $_APP= array_merge($_APP, array(
            'db'=>array(
                'h'=>'localhost',
                'u'=>'root',
                'p'=>'root',
                'd'=>'APpName'
                )
            ));
    
    
    
    
    
    // FUNCTIONS
    
    
    
    
    // HOUSE-KEEPING
    
    ?>
    

    用法:我只需将这个配置文件包含在每个php文件中,然后我就可以使用$_APP['db']['h']

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      相关资源
      最近更新 更多