简体   繁体   English

Slim Framework 删除公用文件夹 URL

[英]Slim Framework remove public folder in URL

There is a similar question here , but I still need to include the public folder in my URL.这里有一个类似的问题,但我仍然需要在我的 URL 中包含public

Current URL: https://my-domain.com/api/public/pins当前 URL: https://my-domain.com/api/public/pins

What I want: https://my-domain.com/api/pins我想要什么: https://my-domain.com/api/pins

This is my project structure这是我的项目结构

在此处输入图像描述

As you can see, I have 2 .htaccess files, where and what should I write a .htaccess script to redirect the public folder?如您所见,我有 2 个 .htaccess 文件,我应该在哪里以及如何编写 .htaccess 脚本来重定向public

.htaccess in public folder public中的.htaccess

# Redirect to front controller
RewriteEngine On

# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

.htaccess in root folder root文件夹中的.htaccess

RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

Here is an example to make an "internal redirect" to the public directory:以下是对公共目录进行“内部重定向”的示例:

RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

The .htaccess in the public/ directory could contain this content: public/目录中的.htaccess可能包含以下内容:

# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

In Slim 4: Make sure to set the correct Slim basePath.在 Slim 4 中:确保设置正确的 Slim basePath。

$app->setBasePath('/api');

See documentation: https://www.slimframework.com/docs/v4/start/web-servers.html#apache-configuration参见文档: https://www.slimframework.com/docs/v4/start/web-servers.html#apache-configuration

In Slim 3: Patch the Slim Environment as follows.在 Slim 3 中:按如下方式修补 Slim 环境。

$container['environment'] = function () {
    $scriptName = $_SERVER['SCRIPT_NAME'];
    $_SERVER['REAL_SCRIPT_NAME'] = $scriptName;
    $_SERVER['SCRIPT_NAME'] = dirname(dirname($scriptName)) . '/' . basename($scriptName);

    return new Slim\Http\Environment($_SERVER);
};

Example 例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM