Search code examples
phpparsingserver-side

Get an activation key string from URI


I have a URL in this format:

https://www.example.com/activate/9ajebaidblahdeblahblah2020

My setup is /activate/index.php

How can I parse /9ajebaidblahdeblahblah2020 with the index.php file in the /activate folder?

I have tried...

$current_url = $_SERVER['REQUEST_URI'];
echo $current_url;

//OR

var_dump($_SERVER['REQUEST_URI']);

What I would like to do is (purpose)...

$current_url = explode("/", $_SERVER['REQUEST_URI']);
//echo $current_url[2];

Thank you ahead of time for any help.


Solution

  • Add a .htaccess file in your activate project folder with the below code:

    RewriteEngine On
    
    RewriteRule ^/?activate/(.+)$ /activate/index.php?val=$1 [NC,L,P]
    

    Demo: https://htaccess.madewithlove.be?share=aa64900e-ba78-4f17-9369-326f4384dd47

    Later in your index.php file, you can just use as:

    <?php
    
    echo $_GET['val'];