how to Mod-rewrite php + Slim url to sub-directory

人盡茶涼 提交于 2019-12-01 11:43:07

问题


I am using Slim 2.4 and php 5.3. My Directory Structure in localhost is something like this :

  1. Web Root
    • AppName
      • API
        • v1
          • include
          • Slim <- Slim Framework
          • tools
          • .htaccess (#1)
          • index.php (#1)
      • Assets
        • css
        • js
        • img
      • .htaccess (#2)
      • index.php (#2)

I wan to access localhost/AppName/v1 that will open up localhost/AppName/API/v1. This is working partially as further parameters are getting ignored. When I am trying to get localhost/AppName/v1/anyOtherVariable it opens up localhost/AppName/API/v1 only.

Now, #1 index.php contains all Slim code (routing and stuff) but #1 .htaccess is blank. #2 index.php contains a simple static homepage that contains some links. In #2 .htaccess I have written

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

I am really out of options.


回答1:


A quick and dirty solution is to define a version constant and then remove it from the global variable $_SERVER['REQUEST_URI']. For example my api url is http://localhost/api/v1/hello/firstname/lastname.

I defined a constant at then removed it from $_SERVER['REQUEST_URI'] as given below:

define('API_VERSION', '/api/v1');

$_SERVER['REQUEST_URI'] = str_replace(API_VERSION, '', $_SERVER['REQUEST_URI']);

$application->get (
    '/hello/:firstname/:lastname',
     ....

And it worked like a charm :)




回答2:


After head-banging 100 times, I decided to change my directory structure and right now, everything is working great. :) I am posting this answer to help others who alos have faced the same problem. So here's, my Directory Structure.

  • WebRoot /
    • AppName /
      • v0.2 /
      • v0.5 /
      • v0.9 /
        • _APP /
          • cache /
          • data /
          • libs /
          • vendor /
            • Slim /
            • userCake /
            • MeekroDB /
        • Acc / ( Seperate Routes )
        • API /
          • .htaccess
          • index.php
        • Assets /
          • images /
          • css /
          • js /
          • fonts /
        • Test /
        • .htaccess
        • 404.php
        • config.php
        • index.php

Now, these are the codes I use inside the main files: index.php under API contains Slim Routes.

.htaccess under APi contains the following code:

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

index.php under v0.9 directory contains the homepage

.htaccess under he same directory contains:

<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>

This .htaccess restricts direct access to _APP folder as it contains sensitive data. Now people will tell me to move it outside the publicly accessible directory but for users with shared hosting like me, the hosting company restricts going outside the public root. Although using this setup secures the App But you should always try to move this _APP directory outside public directory.

and the main config.php contains:

<?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

?>

Usage: I simply Include this config file in every php file and then I can use $_APP['db']['h']



来源:https://stackoverflow.com/questions/22421302/how-to-mod-rewrite-php-slim-url-to-sub-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!