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

柔情痞子 提交于 2019-12-01 11:49:45

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

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

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