CodeIgniter Forums

Full Version: Routes and hashed URLs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I started a projected and noticed that it was getting rather large, so I decided I needed to go to a framework like CI.  So I just set up a new site and I'm setting up the basic functionality but one of the important things about my site is the URLS.  Here's an example:

http://ffbeben.ch/6mqv4mi6wtilgjdgxrgjdg...dgxrgjdgxr

So if you use this URL, and you click on the Head slot and pick an item, the URL will reload to something like this:

http://ffbeben.ch/6mqv4mi6wtilgjdgxr6nyl...dgxrgjdgxr

The reason for this is I want people to be able to build a unit, and then copy and paste the URL to share with other people.

I accomplished this by doing this in the .htaccess

Code:
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/assets
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f



RewriteRule ^(\w+)$ ./index.php?$1

Now moving to CI, I'm a bit at a loss as how to accomplish the same thing.  I've tinkered around with trying to use the same regex in the routes file but I'm not having a whole lot of luck.  Everything I try just results in a 404.

Any advice or guidance would be appreciated.
Personally I use this .htaccess for CI:
Code:
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|vendor|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

resources = images, css, js...
vendor = boostrap, jquery...

You can remap your URL with URI Routing.

application/config/routes.php
PHP Code:
$route['default_controller'] = 'dashboard';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['(:any)'] = 'dashboard/index/$1'

application/controllers/Dashboard.php (case sensitive)
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Dashboard extends CI_Controller
{
 
   public function index($id '')
    {
        echo 
$id;
 
   }


@InsiteFX: I thinks this is more like uniqueness like e.g. pastebin or regex101 does.
(11-22-2017, 04:32 AM)InsiteFX Wrote: [ -> ]You should read this article.

The Comprehensive Guide to URL Parameter Encryption in PHP

You seem to think I'm trying to encrypt or hide something in this URL string and I'm not.  The fact is I'm hashing to make a large number like 405004200 into a smaller set like 6mqv4mi to make the URL smaller.
(11-22-2017, 08:18 AM)wirikidor Wrote: [ -> ]
(11-22-2017, 04:32 AM)InsiteFX Wrote: [ -> ]You should read this article.

The Comprehensive Guide to URL Parameter Encryption in PHP

You seem to think I'm trying to encrypt or hide something in this URL string and I'm not.  The fact is I'm hashing to make a large number like 405004200 into a smaller set like 6mqv4mi to make the URL smaller.

It is unknown how hashing would help you with that though ... Your large number example is certainly smaller than the output size of any reliable hashing function, so I would question if you are indeed "hashing" something or not.

If you are, then the provided article link has some very good advice (despite its title; it actually tells you not to encrypt) and if you aren't then chances are you're doing something wrong (as using the wrong terms to call things is a big sign of that).

The most effective way to shorten a number is to simply convert it to a higher base system, which PHP has a very convenient tool for:

Code:
php > var_dump(base_convert('405004200', 10 /* from base 10 */, 36 /* to base 36; the maximum allowed by this function*/));
php shell code:1:
string(6) "6p4n8o"

Anyway, on topic:

The .htaccess file either works or it doesn't - showing it doesn't give us an idea of what you're trying to accomplish, and you've shown very little detail of what you've tried to do with routes. If you give us an example of which URL should trigger which controller/function, it would be much easier for anyone to help you.

Also, you say 404 but it is unclear whether that's a standard Apache/nginx 404 error page or a CI-styled one? If it is the former, then your .htaccess doesn't work.
I find it odd that I'm asking a question about routing and I'm getting questioned on hashing.  So here's the breakdown:

The game I'm making a calculator for has 10 "slots" for you to fill in items.  Each item and the unit itself is represented by a number like 504010010.  I get these numbers from JSON files. The GIT repository if you're curious where the data comes from (https://github.com/aEnigmatic/ffbe).

So, I have 11 "slots" so to speak to make up the URL.  The goal is to make the URL as short as possible without using a database/association table.  Otherwise I'd have really small URLs which would be awesome.

So let's take one build for example.

3020014061100000072999999999404002400408001400409004800409004800504203380504203380504203380504203380

That's the string before hashing.

4zsxryi6wthkgjdgxr6oj68w6qwvw86rie4g6rie4g8c6ttg8c6ttg8c6ttg8c6ttg

That's the string after hashing.

Here's the code how I do it:

Code:
$unit = base_convert(substr($request,0,6), 36, 10);
$right = base_convert(substr($request,6,6), 36, 10);
$left = base_convert(substr($request,12,6), 36, 10);
$head = base_convert(substr($request,18,6), 36, 10);
$body = base_convert(substr($request,24,6), 36, 10);
$acc1 = base_convert(substr($request,30,6), 36, 10);
$acc2 = base_convert(substr($request,36,6), 36, 10);
$materia1 = base_convert(substr($request,42,6), 36, 10);
$materia2 = base_convert(substr($request,48,6), 36, 10);
$materia3 = base_convert(substr($request,54,6), 36, 10);
$materia4 = base_convert(substr($request,60,6), 36, 10);

Again, the goal here is to make the URL as short as possible.  Not trying to encrypt or hide anything.

As for the routing issue: The .htaccess file I posted is how I'm doing it now without using CodeIgniter.  I was attempting to show how I was successful in doing what I wanted to do before, but trying to switch to the CI framework I was having difficulty accomplishing the same thing.
Yea, that's not hashing ... don't call it that way, terms mean stuff. And you don't really need all of those function calls. base_convert() can handle extremely large numbers, just call it once on the entire string of "slots" and it will be even more efficient - 64 instead of the 66 chars shown in your post.

Anyway, that .htaccess file just redirects $uri to index.php/$uri, which is fine, but framework-level routing is a different thing.

@jreklund's suggestion should get you what you want.
@jreklund
This is exactly what I needed.  It worked like a champ.  Thank you so much for taking the time to actually read what I was trying to accomplish and leading me down the right path.  You're the best.