Skip to content

Instantly share code, notes, and snippets.

@im-noob
Created March 5, 2021 23:58
Show Gist options
  • Save im-noob/7c26e72eebfc5eaf88a7b35508593861 to your computer and use it in GitHub Desktop.
Save im-noob/7c26e72eebfc5eaf88a7b35508593861 to your computer and use it in GitHub Desktop.
Redirection in php
# index.php
<?php
$REQUEST_URI = $_SERVER['REQUEST_URI'];
$REQUEST_URI_split = explode('/',$REQUEST_URI);
$SCRIPT_NAME = $_SERVER['SCRIPT_NAME'];
$SCRIPT_NAME_split = explode('/',$SCRIPT_NAME);
$intersection_length = count(array_intersect($REQUEST_URI_split,$SCRIPT_NAME_split));
$paramter_given = array_slice(
$REQUEST_URI_split,
$intersection_length,
count($REQUEST_URI_split)-$intersection_length
);
print_r(implode('/', $paramter_given));
?>
# .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment