0

I'm using Codeigniter in my new web application, and I have a form in page which sends data via post to a server and that server returns the users back to my website but with the parameters via get with ugly links like example.com/id=12&code=Xxxx

What I would like to do, if it's possible and I've been searching and I can't find is how to convert those ugly links into nice friendly links ( example.com/12/Xxxx )

Thanks

Hailwood
  • 89,623
  • 107
  • 270
  • 423
user1524462
  • 95
  • 2
  • 10

2 Answers2

4

You can't have a GET form transform into a nice URL directly. They will automatically become ?key=val format.

Your best option is to have a redirect header to translate the GET to the nice URL's.

eg:

$firstPart = $_GET['myKey'];
$secondPart = $_GET['mySecondKey'];
header('Location: ' . $requestURL . '/' . $firstPart . '/' . $secondPart);
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
2

Get parameters are basically standard for any Web API's so you will probably find that you will end up using them a lot so rather than hack fixes for each and every API create a middle man.

Create a new folder at the same level as your index.php (usually your site root). call this folder something like middleman

Add the middleman folder to the list of items in your .htaccess file that should not be routed through the index.php

Now for every API you use you can create a new .php file in the middleman folder which transforms all requests to one that your site can understand.

You then point your external API's to http://yoursite.com/middleman/api_name.php

The reason for creating the middleman is that Code Igniter obliterates the get array so it gets removed before you can process the requests and format them into something meaningful if you try to do it inside Code Igniter so we must perform the transformation outside of Code Igniter's scope.

an example file might looks like this:
mysite.pingback.php

<?php
    $from = $_GET['ip'];
    $time = $_GET['time'];
    $post = $_GET['id'];

    header('location: ../mysite/pingback/'.$from.'/'.$post.'/'.$time);
?>

So very simple files. but it also means that when a change is made to the external API it does not affect how your Code Igniter app works as you simple have to change your middleman.

Hailwood
  • 89,623
  • 107
  • 270
  • 423