Forum Discussion

Doug996's avatar
Doug996
Copper Contributor
Mar 15, 2022

How can I use dynamic paths that all load the same file?

Hello.  I was doing a project where I basically make my own TinyURL in PHP.  Problem is, I hit a wall when I went to actually implement it.  In TinyURL, if I enter a URL I may end up with something like https://tinyurl.com/4868s6va.  If I do that with my own domain, problem is when the user uses the link, IIS will attempt to load a default file from directory 4868s6va.  I need IIS to look for the default file at the apex domain and pass the path to it as a parameter.  So even if I were to do https://www.mydomain.com/some/random/path, it would still actually load https://www.mydomain.com/index.php and pass it some/random/path somehow.

 

How does one accomplish that?  I mean, I suppose I could just create a folder and put a redirect in the folder to load when you load that path, but I absolutely 100% cannot believe that is how it should be done.  That's horridly inefficient, lol.

1 Reply

  • Strahan201's avatar
    Strahan201
    Copper Contributor

    Install the URL Rewrite module then go into the IIS Management application. Click the website in question. Open URL Rewrite. Create a new rule.

     

    Requested URL: Does not match the pattern
    Using: Regular expressions
    Pattern: \/(?!.*index|$)
    Ignore case: true
    Action: Rewrite
    Rewrite URL: index.php?tk=1
    Append query string: true
    Stop processing of subsequent rules: true

     

    This assumes you use index.php as your script file. If you use default.php, just change the index to default in the regex.  If you had this as your index.php:

     

    <?php
    
    echo array_key_exists("REQUEST_URI", $_SERVER) ? $_SERVER["REQUEST_URI"] : "No URI";
    
    ?>

     

    then you went to https://www.mydomain/com/whatever, you would get this output:

     

    /whatever

     

    Which you can then use to query the database for the appropriate URL to push.  In my rewrite URL example I did index.php?tk=1 so you can look for $_REQUEST["tk"] to know this is an attempt to do a saved URL, not simply normal use of the site.  Obv change that to whatever "fingerprint" format you wish.

Resources