如何mod-rewritePHP +修身网址子目录子目录、网址、mod、rewritePHP

2023-09-02 20:32:21 作者:≡儘歡未ɡοц

我使用的超薄2.4和PHP 5.3。在本地主机我的目录结构是这样的

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

在Web根目录 AppName的 API 在V1 包含 在超薄&LT; - 超薄框架 工具 的.htaccess(#1) 在index.php文件(#1) Web Root AppName API v1 include Slim <- Slim Framework tools .htaccess (#1) index.php (#1) CSS JS​​ IMG

我婉访问本地主机/ AppName的/ V1 ,将打开的本地主机/ AppName的/ API / V1 。这是工作部分为更多的参数越来越忽略。当我试图让本地主机/ AppName的/ V1 / anyOtherVariable 它开辟了本地主机/ AppName的/ API / V1 只。

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.

现在,#1 index.php文件包含所有修身code(路由和东西),但#1的.htaccess是空白。  #2 index.php文件包含一个简单的静态网页,其中包含一些链接。在#2的.htaccess我写

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.

推荐答案

在撞头的 100次,我决定改变我的目录结构,而现在,一切是伟大的工作。 :)我张贴这个答案,以帮助其他人谁ALOS都面临着同样的问题。因此,这里是我的目录结构。

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 / 缓存/ 数据/ 库/ 供应商/ 修身/ userCake / MeekroDB / WebRoot / AppName / v0.2 / v0.5 / v0.9 / _APP / cache / data / libs / vendor / Slim / userCake / MeekroDB / 的.htaccess 的index.php 图片/ CSS / JS​​ / 字体/

现在,这些都是codeS我用里面的主要文件: 根据API的index.php包含超薄路线。

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

受API的.htaccess包含以下code:

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

V0.9目录下的index.php包含网页

index.php under v0.9 directory contains the homepage

他同一个目录下的.htaccess包含:

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

本的.htaccess限制直接访问_APP文件夹,因为它包含敏感数据。现在,人们会告诉我将它移到外面可公开访问的目录,但为用户提供共享主机和我一样,托管公司限制了将公共根外。虽然使用这种设置保证了应用程序,但你应该总是尝试将这种_APP目录公开目录之外。

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.

和主config.php文件包括:

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

?>

用法:我只需在每个PHP文件中这个配置文件,然​​后我可以使用 $ _ AP​​P ['数据库'] ['H']

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