Home > Blockchain >  URL redirect to use subdirectory as GET parameter
URL redirect to use subdirectory as GET parameter

Time:03-20

I imagine this has to be a common scenario but I'm struggling to describe it sufficiently well or to find a working answer!

Essentially I want to make hundreds of URLS that include unique reference codes but that are easy to type in the form example.com/aabbcc, which will be intercepted and all delivered to a PHP script for validating that code, located somewhere like example.com/script.php. I need the subdirectory part of the URL (aabbcc, in this example) to become a GET parameter for that script, so a URL like the one above would be sent to example.com/script.php?id=aabbcc, while hiding this more complicated URL from the user.

I can see from other .htaccess examples that this must be possible, but I can't find one doing this.

Is there a .htaccess solution for it? Is there something else even more basic? Your help is appreciated in steering me.

CodePudding user response:

If your "unique reference codes" consist of 6 lowercase letters, as in your example then you can do something like the following in your root .htaccess file using mod_rewrite:

RewriteEngine

# Internally rewrite "/abcdef" to "script.php?id=abcdef"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[a-z]{6}$ script.php?id=$0 [L]

If you don't need direct access to any subdirectories off the root that also happen to match a "unique reference code" then you can remove the preceding condition (RewriteCond directive). With the condition in place then you naturally can't access any "unique access codes" that happen to also match the name of a subdirectory.

$0 is a backreference to the entire URL-path that the RewriteRule pattern (first argument) matches against.


Reference

  • Related