Giter Site home page Giter Site logo

settermjd / regex-redirector Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 9 KB

This is a simple package that simplifies the process of performing redirects by searching and replacing a pattern within the URL, if present. It's not meant to be, in any way, sophisticated.

PHP 100.00%

regex-redirector's Introduction

Regex Redirector

This is a simple package that simplifies the process of performing redirects by searching and replacing a pattern within the URL, if present. It's not meant to be, in any way, sophisticated.

Installing

To install the module, use Composer by running composer require settermjd/regex-redirector.

Getting Started

The constructor takes two arguments:

  1. An array. This is a simple key/value list of patterns to look for with patterns to replace
  2. The currently requested URL.

The package provides two functions:

  • requiresRedirect(): This tests if the requested URL requires a redirect
  • getRedirectUrl(): Retrieves the URL that the current request should be redirected to

If you just want to test, use the first, if you want to redirect, use the second, which uses the first internally.

After initializing the object, pass the return value from getRedirectUrl() to PHP's header function, as in the example below, and the request will be redirected.

<?php

require_once('vendor/autoload.php');

$redirectList = [
    'configuration_server' => 'configuration/server'
];

$requestUrl = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$redirector = new RegexRedirector\Redirector($redirectList, $requestUrl);

if ($redirector->requiresRedirect()) {
    header(sprintf('Location: %s', $redirector->getRedirectUrl()));
    exit;
}

Running the Tests

To run the unit tests, run composer test.

regex-redirector's People

Contributors

settermjd avatar

Stargazers

Hannah Chartier avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.