PHPackages                             selective/basepath - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. selective/basepath

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

selective/basepath
==================

A URL base path detector for Slim 4

2.4.0(3mo ago)50521.0k—8.5%7[1 issues](https://github.com/selective-php/basepath/issues)11MITPHPPHP 8.1.\* || 8.2.\* || 8.3.\* || 8.4.\* || 8.5.\*CI passing

Since Aug 21Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/selective-php/basepath)[ Packagist](https://packagist.org/packages/selective/basepath)[ Docs](https://github.com/selective-php/basepath)[ RSS](/packages/selective-basepath/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (7)Versions (10)Used By (11)

selective/basepath
==================

[](#selectivebasepath)

A URL base path detector for Slim 4.

[![Latest Version on Packagist](https://camo.githubusercontent.com/3cb7d76a0d3e08314d25566abd5628ed788cd3ef1350c58675537c394a094750/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73656c6563746976652d7068702f62617365706174682e737667)](https://packagist.org/packages/selective/basepath)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)[![Build Status](https://github.com/selective-php/basepath/workflows/build/badge.svg)](https://github.com/selective-php/basepath/actions)[![Total Downloads](https://camo.githubusercontent.com/4b0837c0dbdf8cc86b8ed6b0ae3bac0bdbe35d2c54d349dbdfce653f7cb53094/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73656c6563746976652f62617365706174682e737667)](https://packagist.org/packages/selective/basepath/stats)

Features
--------

[](#features)

- Support for Apache and the PHP built-in webserver
- Tested
- No dependencies
- Very fast

### Supported servers

[](#supported-servers)

- Apache webserver with mod\_rewrite and .htaccess
- PHP build-in webserver

Requirements
------------

[](#requirements)

- PHP 8.2 - 8.5

Installation
------------

[](#installation)

```
composer require selective/basepath

```

The recommended **directory structure**:

- `public/` Web server files, the [DocumentRoot](https://httpd.apache.org/docs/2.4/de/mod/core.html#documentroot)
    - `.htaccess` Apache redirect rules for the front controller
    - `index.php` The front controller
- `.htaccess` Internal redirect to the public/ directory

The following steps are necessary for your Slim 4 application:

For **Apache** we have to "redirect" the web traffic to the front controller in `public/index.php`.

Create a file: `public/.htaccess` with this content:

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

```

We also need a rule to "redirect" the sub-directories to the front-controller in `public/index.php`.

Create a second `.htaccess` file above the `public/` directory with this content:

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

```

Usage
-----

[](#usage)

### Slim 4 integration

[](#slim-4-integration)

Add the `BasePathMiddleware` after `addRoutingMiddleware()` to set the basePath before the routing is started.

Example: `public/index.php`

```
