Welcome Guest, Not a member yet? Register   Sign In
Rewrite URL's
#1

[eluser]Guido Mallee[/eluser]
Hi,

Im building a webshop on CI and I'm implementing a paymenth method called iDEAL, which is a Dutch payment method. During the checkout proces, the visitor leaves the website to do the iDEAL transaction, then iDEAL sends the visitor back to the webshop like this:

http://www.mywebshop.nl/checkoutcomplete...c7b72e610d

Since I'm using seo friendly url's, CI doesnt allow this url. I would like to rewrite the url to the following:

http://www.mywebshop.nl/checkoutcomplete...c7b72e610d

I've tried to edit my .htaccess but I didn't even manage to make a simple rewrite from for example product.php?id=12 to product/12

This is my .htaccess file:

Code:
ErrorDocument 404 /index.php
DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>

Thanks in advance!
#2

[eluser]BrianDHall[/eluser]
Just as an alternative, you could enable query strings, probably need to set uri_protocol to PATH, and just use regular rewrites inside CI to handle this in the routes.php file.
#3

[eluser]Guido Mallee[/eluser]
Thanks for your tip. Seems like a perfect alternative. But how do i do the regular rewrite part? When i use a '?' in my route index, CI says no...
#4

[eluser]JoostV[/eluser]
Hi Guido, you can easily tackle this in two ways:

1. Ugly hack, but works fine - in the top of your index.php, store the GET vars in global constants:
Code:
define('C_TRIXID', (int) $_GET['trxid']);
define('C_EC', $_GET['ec']);

2. Do not define these globals by hacking your index.php, but define them in a pre_system hook instead.
Add to config/hooks.php:
Code:
$hook['pre_system'] = array(
                            'class'    => 'ideal_hook',
                            'function' => 'get_to_global',
                            'filename' => 'ideal_hook.php',
                            'filepath' => 'hooks',
                            );
Hook class:
Code:
/**
* This hook is called pre_system, before the GET array is killed by CI
*/
class ideal_hook
{

    /**
     * Store every GET var as a global constant
     * E.g. $_GET['trixid'] becomes C_TRIXID
     * @return void
    */
    public function get_to_global ()
    {
        foreach ($_GET as $key => $value) {
            define('C_' . strtoupper($key), html_entities($value));
        }
    }
}

So, no need for .htaccess modifications or enabling GET.
#5

[eluser]Guido Mallee[/eluser]
Thanks! I'll try this the first thing tomorrow! But this way, the url itself doesn't change, does it? I could redirect then of course to a seo friendly url, but is there an other way to acomplish this?
#6

[eluser]BrianDHall[/eluser]
[quote author="Guido Mallee" date="1257302662"]Thanks for your tip. Seems like a perfect alternative. But how do i do the regular rewrite part? When i use a '?' in my route index, CI says no...[/quote]

Here you go: http://ellislab.com/forums/viewthread/133908/

Basically what I do is in my routes.php file I check $_GET - which still exists because I enable query strings.

The nice thing is that you aren't forced to use GET - it just becomes an option you can freely intermix with CI-style url segments.

If any of this doesn't make sense just let me know and I'll try to be a little more specific.

The use of conditionally setting routes based upon GET usage allows you to accomplish things you otherwise have to do a lot of work to accomplish. As the hook method demonstrates there is more than one way to handle things, but I really like the flexibility of handling GET and POST - I really think it is how CI should be configured by default.
#7

[eluser]JoostV[/eluser]
@guido: no, the url stays like http://www.mywebshop.nl/checkoutcomplete...c7b72e610d

But like Brian says, there are more than one way to tackle this. His way is probably more generic.
#8

[eluser]Guido Mallee[/eluser]
Ok, no problem. For now I'll use this method then, so i can continue the project, but i still hope someone can tell me how I can change the url itself..
#9

[eluser]JoostV[/eluser]
You might find the answer here
#10

[eluser]Guido Mallee[/eluser]
Thanks, I found the answer there indeed!

For those who want to know how I resolved my problem, I adjusted my .htaccess file to this:

Code:
ErrorDocument 404 /index.php
DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{QUERY_STRING} ^trxid=([0-9]*)&ec;=(.*)$
    RewriteRule ^checkout/complete/$$ http://www.mywebshop.com/checkout/complete/%1/%2? [R=302,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>




Theme © iAndrew 2016 - Forum software by © MyBB